2

I'm using a method named: Image_resize to resizing my images, but when I call this method, I get:

ArgumentException: Parameter is not valid. System.Drawing.SafeNativeMethods+Gdip.CheckStatus(int status)

Stack Query Cookies Headers Routing ArgumentException: Parameter is not valid. System.Drawing.SafeNativeMethods+Gdip.CheckStatus(int status) System.Drawing.Bitmap..ctor(string filename, bool useIcm) System.Drawing.Bitmap..ctor(string filename) Keyhanatr.Core.ImageMethods.ImageConvertor.Image_resize(string input_Image_Path, string output_Image_Path, int new_Width) in ImgMehods.cs + Bitmap source_Bitmap = new Bitmap(input_Image_Path); Keyhanatr.Core.ImageMethods.ImgMehods.CreateProductImg(IFormFile imgUp) in ImgMehods.cs + ImageConvertor.Image_resize(imgPath,thumbPath,150); Keyhanatr.Core.Services.Products.ProductServices.AddProduct(Product product, IFormFile imgUp) in ProductServices.cs + product.ImageName = ImgMehods.CreateProductImg(imgUp); Keyhanatr.Areas.Admin.Controllers.ProductsController.Create(Product product, IFormFile imgUp) in ProductsController.cs + _productServices.AddProduct(product, imgUp);

Also getting green squiggle under some lines. The used method is as fallowing :

public static class ImageConvertor
{
    public static void Image_resize(string input_Image_Path, string output_Image_Path, int new_Width)
    {
        const long quality = 50L;
        Bitmap source_Bitmap = new Bitmap(input_Image_Path);
        double dblWidth_origial = source_Bitmap.Width;
        double dblHeigth_origial = source_Bitmap.Height;
        double relation_heigth_width = dblHeigth_origial / dblWidth_origial;
        int new_Height = (int)(new_Width * relation_heigth_width);

        //< create Empty Drawarea >
        var new_DrawArea = new Bitmap(new_Width, new_Height);
        //</ create Empty Drawarea >

        using (var graphic_of_DrawArea = Graphics.FromImage(new_DrawArea))
        {
            //< setup >
            graphic_of_DrawArea.CompositingQuality = CompositingQuality.HighSpeed;
            graphic_of_DrawArea.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphic_of_DrawArea.CompositingMode = CompositingMode.SourceCopy;
            //</ setup >

            //< draw into placeholder >
            //*imports the image into the drawarea
            graphic_of_DrawArea.DrawImage(source_Bitmap, 0, 0, new_Width, new_Height);
            //</ draw into placeholder >

            //--< Output as .Jpg >--
            using (var output = System.IO.File.Open(output_Image_Path, FileMode.Create))
            {
                //< setup jpg >
                var qualityParamId = System.Drawing.Imaging.Encoder.Quality;
                var encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality);
                //</ setup jpg >

                //< save Bitmap as Jpg >
                var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
                new_DrawArea.Save(output, codec, encoderParameters);
                //resized_Bitmap.Dispose ();
                output.Close();
                //</ save Bitmap as Jpg >
            }
            //--</ Output as .Jpg >--
            graphic_of_DrawArea.Dispose();
        }
        source_Bitmap.Dispose();
        //---------------</ Image_resize() >---------------
    }
}

It's that I was using this method in other project before and it was working well, but I don't know why it doesn't work here at this project! Anyone can help?

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Hossein
  • 142
  • 10
  • what operating system ? – Daniel A. White Nov 13 '21 at 17:37
  • @DanielA.White Windows – Hossein Nov 13 '21 at 17:40
  • The exception is coming from `System.Drawing.Bitmap..ctor(string filename)`, it seems to be an invalid image file or with some format that .NET doesn't understand (maybe wrong extension?) – Steeeve Nov 13 '21 at 17:40
  • Enable displaying the warnings in the error output window to see what the green squiggles mean, or just hover with the mouse over them to display a tooltip. – Olivier Jacot-Descombes Nov 13 '21 at 17:43
  • @OlivierJacot-Descombes I just hoverd on `Bitmap` and it said: **This call site is reachable on all platforms. 'Bitmap' is only supported on: 'Windpws'** – Hossein Nov 13 '21 at 17:53
  • See: [How to fix C# Warning CA1416 in vscode?](https://stackoverflow.com/a/66735113/880990). – Olivier Jacot-Descombes Nov 13 '21 at 17:55
  • @OlivierJacot-Descombes Where or how do I must use it? Because I just tried the way in the given link above but it doesn't made any difference and I got the same exception again! – Hossein Nov 13 '21 at 18:14
  • 1
    The squiggles have nothing to do with the exception. Have you checked the file that causing the exception? Can you open it with whatever image viewer? – Steeeve Nov 13 '21 at 18:36
  • @Steeeve There is no wrong with the uploaded file – Hossein Nov 13 '21 at 19:31
  • @Steeeve When debugging, it stops on `double dblWidth_origial = source_Bitmap.Width;` and sayes: **user UnHandeled exception** :( – Hossein Nov 13 '21 at 19:33
  • The exception you posted is another one. What are the details of the new one? – Steeeve Nov 13 '21 at 19:40

1 Answers1

0

I met the same problem, and found that you should check width and height not zero first.

var new_DrawArea = new Bitmap(new_Width, new_Height)

Vincent
  • 3,124
  • 3
  • 21
  • 40