2

I've been trying to change the size of my photo. I've looked at a lot of sites: 1 , 2 , 3.

The last link (3) at least will resize the image except the old image remains behind the resized one. How is this happening? And why? If i minimize the winform and then bring the winform back up, the old image is gone and i'm left with just the resized version. How do i get rid of the old image?

Any ideas?

Community
  • 1
  • 1
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • There is nothing wrong with your resize method. – doogle Jan 22 '12 at 17:46
  • 2
    *Never* use CreateGraphics(), it doesn't properly erase the background and whatever you draw will disappear easily. Implement the Paint event instead and use the passed e.Graphics. Your Click event should call Invalidate() to trigger a paint. – Hans Passant Jan 22 '12 at 17:51
  • Maybe you could post a code sample so we can see what you're doing. – Chris Pietschmann Jan 22 '12 at 21:11
  • @Hans Passant I wish you would have posted your answer instead of leaving it as a comment. you were totally right!!! – BigBug Jan 23 '12 at 02:44

2 Answers2

1

That first link from C# Tutorial - Image Editing: Saving, Cropping, and Resizing gave you the code.

Is there a reason not to use it?

private static Image resizeImage(Image imgToResize, Size size)
{
   int sourceWidth = imgToResize.Width;
   int sourceHeight = imgToResize.Height;

   float nPercent = 0;
   float nPercentW = 0;
   float nPercentH = 0;

   nPercentW = ((float)size.Width / (float)sourceWidth);
   nPercentH = ((float)size.Height / (float)sourceHeight);

   if (nPercentH < nPercentW)
      nPercent = nPercentH;
   else
      nPercent = nPercentW;

   int destWidth = (int)(sourceWidth * nPercent);
   int destHeight = (int)(sourceHeight * nPercent);

   Bitmap b = new Bitmap(destWidth, destHeight);
   Graphics g = Graphics.FromImage((Image)b);
   g.InterpolationMode = InterpolationMode.HighQualityBicubic;

   g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
   g.Dispose();

   return (Image)b;
}

Don't ignore the InterpolationMode. It will make the image better looking.

Also, I don't think you want to use CreateGraphics() in this context. You probably want to use an actual bitmap to store the resized image in or just use the Graphic object from the paint event of the control you want to show the image in.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • i've tried adding this into my code, but it won't work, i'd have to make sure StartPainting gets the return image (which if i do, it compiles and runs with no errors but the image doesn't change size? (not sure why) ..... – BigBug Jan 22 '12 at 17:49
  • @BlueMonster What's your original image? It looks like you're trying to just resizing an image from a control, not an original bitmap. – LarsTech Jan 22 '12 at 17:53
  • i have an image loaded in using OpenFileDialog... i play with the bits to change it's colour etc. I'm using Bitmap ... to play with the colours i have to lock the image into memory and then i unlock using "UnlockBits()" that all seems to be working fine... it's just resize, i'm not sure how to go about playing with the bits to resize my image? – BigBug Jan 22 '12 at 17:55
  • I'll add the loading of the image and an example of changing the colour of the image to my code above so you can see.. – BigBug Jan 22 '12 at 17:56
  • @BlueMonster Yeah, there's just a lot of problems. You are creating a picturebox, but you aren't using it, nor does it get added to any form. What are you trying to draw on? The form? A picturebox? Some other control? – LarsTech Jan 22 '12 at 19:45
  • umm? my program does work! Actually, i just realized, it has something to do with not clearing the image... If i resize the winform, the old image gets cleared and the resized image is the only thing left.. so.. i just have to find out how to clear that image instead of waiting for the user to minimize the winform.. – BigBug Jan 22 '12 at 19:52
  • yes, you're right about the picture box, i've gotten rid of it in my code... it doesn't change the functionality though.. – BigBug Jan 22 '12 at 19:54
  • Try to use myPictureBox.Invalidate() method to refresh the image shown in the PitureBox. – Lukasz M Jan 22 '12 at 20:37
  • @BlueMonster What your code still doesn't tell me is *where* you are trying to paint this resized image, because that is the control that you should be invalidating, and that control should have the paint event where your graphic object should be coming from. Not from CreateGraphics(). – LarsTech Jan 22 '12 at 23:53
  • I figured it out just now.. i was painting right on the winform, instead now i'm painting on a panel and i just call the panels invalidate() and everything works beautifully. Thanks anyway though.. – BigBug Jan 23 '12 at 02:11
1

Aren't you clearing surface of graphics object? maybe because of that old image still might have been showing. take a look at here http://msdn.microsoft.com/en-us/library/system.drawing.graphics.clear.aspx

DSharper
  • 3,177
  • 9
  • 29
  • 47
  • Hmm i tried adding in " e.Graphics.Clear(Color.White); " but the un-resized image still remains behind? Is there something i can use to clear that image out? – BigBug Jan 22 '12 at 17:51