locked
Resizing image not working in C# RRS feed

  • Question

  • User936012285 posted

    Hi,

    I am trying to resize an image to 400 * 400 pixels.

    When I save the image the image is not resizing. The size remains the same as of original code.

    could you please check what is wrong with the code.

    The original image size is 300 * 200 pixels.

     

     

    void test()

    {

    using (WebClient c = new WebClient())

    {

    using (MemoryStream ms = new MemoryStream(c.DownloadData("http://test12345.com/testimage.jpg")))

    {

    System.Drawing.Image i = System.Drawing.Image.FromStream(ms);

    Graphics graphic = Graphics.FromImage(i);

    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;

     

    graphic.DrawImage(i, 0, 0, 400, 400);

    graphic.Save();

    i.Save("C:\\MyImage77777.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    i.Dispose();

    ms.Close();

      }

     } 

    }

    Wednesday, February 23, 2011 2:02 PM

Answers

  • User1508394307 posted

    You need to resize the source image like this

    System.Drawing.Image original_size = System.Drawing.Image.FromStream(ms);
    System.Drawing.Image i = new Bitmap(original_size, 400, 400);
    
    Graphics graphic = Graphics.FromImage(i);
    ...

    and so basically there is no need to set 400x400 in the DrawImage method, which you can change to:

    graphic.DrawImage(i, 0, 0);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 23, 2011 2:14 PM
  • User1508394307 posted

    Just add following lines after * crop * into your code

    System.Drawing.Image original_size = System.Drawing.Image.FromStream(ms);
    System.Drawing.Image i = new Bitmap(original_size, 400, 400);
    Graphics graphic = Graphics.FromImage(i);
    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
    graphic.DrawImage(i, 0, 0, 400, 400);
    graphic.Save();
    
    /* crop */
    MemoryStream imgMemoryStream = new MemoryStream();
    Bitmap bmp = new Bitmap(75, 75, PixelFormat.Format24bppRgb);
    Graphics graphic_crop = Graphics.FromImage(bmp);
    
    graphic_crop.DrawImage(i, new Rectangle(0, 0, 75, 75), 0, 0, 75, 75, GraphicsUnit.Pixel);
    bmp.Save("C:\\MyImage77777.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    where 75x75 is new size.

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 27, 2011 3:43 PM
  • User1508394307 posted

    If I understood you correct you need to set your coordinates here

    graphic_crop.DrawImage(i, new Rectangle(0, 0, 75, 75), x, y, 75, 75, GraphicsUnit.Pixel);
    where x set to 13 and y to 88.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 4:07 AM

All replies

  • User1508394307 posted

    You need to resize the source image like this

    System.Drawing.Image original_size = System.Drawing.Image.FromStream(ms);
    System.Drawing.Image i = new Bitmap(original_size, 400, 400);
    
    Graphics graphic = Graphics.FromImage(i);
    ...

    and so basically there is no need to set 400x400 in the DrawImage method, which you can change to:

    graphic.DrawImage(i, 0, 0);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 23, 2011 2:14 PM
  • User936012285 posted

    Thanks a ton!!!! 

     Works cool.

     

    Wednesday, February 23, 2011 2:53 PM
  • User936012285 posted
    thanks for the info. I have another problem now I want to resize and image and then crop it and then only save on the disk. I am able to resize or crop but not able to do both. could you please help
    Friday, March 25, 2011 11:58 AM
  • User1508394307 posted

    Just add following lines after * crop * into your code

    System.Drawing.Image original_size = System.Drawing.Image.FromStream(ms);
    System.Drawing.Image i = new Bitmap(original_size, 400, 400);
    Graphics graphic = Graphics.FromImage(i);
    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
    graphic.DrawImage(i, 0, 0, 400, 400);
    graphic.Save();
    
    /* crop */
    MemoryStream imgMemoryStream = new MemoryStream();
    Bitmap bmp = new Bitmap(75, 75, PixelFormat.Format24bppRgb);
    Graphics graphic_crop = Graphics.FromImage(bmp);
    
    graphic_crop.DrawImage(i, new Rectangle(0, 0, 75, 75), 0, 0, 75, 75, GraphicsUnit.Pixel);
    bmp.Save("C:\\MyImage77777.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    where 75x75 is new size.

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 27, 2011 3:43 PM
  • User936012285 posted
    Hi Smimov, It works really really well. Thanks a lot for your help. Much appreciated. you are .net champ !!!!
    Sunday, March 27, 2011 7:37 PM
  • User936012285 posted

    Hi smirnov,

    thanks, I have got another minor issue now.

    When I crop to 100 by 100 image to 75 * 75,

    I want to start from 13th pixel (from width)  and till 88th pixel (of the 100 pixel image).

    But it is leaving some black area on the left or right.

    could you please help.

     

    Monday, March 28, 2011 3:56 AM
  • User1508394307 posted

    If I understood you correct you need to set your coordinates here

    graphic_crop.DrawImage(i, new Rectangle(0, 0, 75, 75), x, y, 75, 75, GraphicsUnit.Pixel);
    where x set to 13 and y to 88.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 4:07 AM
  • User936012285 posted

    Hi Smirnov,

    I think the problem is that if I resize and then save it on to the disk and then fetch the image again and  crop it then it is fine

    But if I just save it in memory using graphic.save() and then try to crop it, it uses the original image size and not the resized one to crop.

    I am using the code you gave me to crop but graphic.save() does not seem to work .

    Could you please advise.

     

    Monday, March 28, 2011 5:21 AM
  • User936012285 posted

    Hi Smirnov,

    I figured it out. I am now just saving it to a memorystream and then picking it in image object and then doing the cropping as per your code and it works wonders.

    Thanks for the solution, you are a great help.!!!!!

     

     

    Monday, March 28, 2011 6:24 AM