Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image.Save Parameter not valid

I want to watermark files in a folder with another image. However, am getting the error

'Parameter Not Valid'

when invoking the code

img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);

I have the following code;

    public static string WatermarkImagesInFolder(string url)
    {
        if (url == null)
            throw new Exception("URL must be provided");

        string path = HttpContext.Current.Server.MapPath(url);

        if (!Directory.Exists(path))
            throw new DirectoryNotFoundException();

        Directory.CreateDirectory(String.Format(@"{0}\watermarked", path));

        List<string> urls = GetJpgFilesFromFolder(path);

        foreach (string imageUrl in urls)
        {
            Image img = WatermarkImage(imageUrl);

            string filename = Path.GetFileName(imageUrl);
            string filepath = String.Format(@"{0}\watermarked\{1}", path, filename);
            img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        return "complete";
    }

and

    public static Image WatermarkImage(string filename)
    {
        using (Image image = Image.FromFile(filename))
        using (Image watermarkImage = Image.FromFile(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["WatermarkImageUrl"])))
        using (Graphics imageGraphics = Graphics.FromImage(image))
        using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
        {
            int x = (image.Width / 2 - watermarkImage.Width / 2);
            int y = (image.Height / 2 - watermarkImage.Height / 2);
            watermarkBrush.TranslateTransform(x, y);
            imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
            return image;
        }
    }

as an example of input for img.save;

img.save("C:\\IMAGES\\wateremarked\\IMAGE (1).jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

any ideas on what could be causing the error here please?

like image 947
Matthew Flynn Avatar asked Feb 07 '23 09:02

Matthew Flynn


1 Answers

In your WatermarkImage you have the image object in a using statement. As soon as that goes out of scope, Dispose is invoked on the image. You would need to return a copy of the image - or not use the using statement & ensure you dispose properly later.

See : Exception: Parameter is not valid (on passing new image to pictureBox)

public static string WatermarkImagesInFolder(string url)
{
    if (url == null)
        throw new Exception("URL must be provided");

    string path = HttpContext.Current.Server.MapPath(url);

    if (!Directory.Exists(path))
        throw new DirectoryNotFoundException();

    Directory.CreateDirectory(String.Format(@"{0}\watermarked", path));

    List<string> urls = GetJpgFilesFromFolder(path);

    foreach (string imageUrl in urls)
    {
        using(Image img = WatermarkImage(imageUrl))
        {
        string filename = Path.GetFileName(imageUrl);
        string filepath = String.Format(@"{0}\watermarked\{1}", path, filename);
        img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }

    return "complete";
}

public static Image WatermarkImage(string filename)
{
    Image image = Image.FromFile(filename);
    using (Image watermarkImage = Image.FromFile(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["WatermarkImageUrl"])))
    using (Graphics imageGraphics = Graphics.FromImage(image))
    using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
    {
        int x = (image.Width / 2 - watermarkImage.Width / 2);
        int y = (image.Height / 2 - watermarkImage.Height / 2);
        watermarkBrush.TranslateTransform(x, y);
        imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
        return image;
    }
}
like image 181
PaulF Avatar answered Feb 12 '23 10:02

PaulF