Jump to content
  • Advertisement
Sign in to follow this  
u235

[.net] resizing images

This topic is 4585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this code that I got from an article off of The Code Project on resizing an image, but I am having a slight problem. Resizing to a smaller size works fine and resizing from smaller than original to original size works fine as well. But what happens when trying to go from any size to larger than original size, is nothing. Nothing happens, and I was wondering if anyone could find an explanation as to why. Thanks. Here's the code:
            float scaleX = (float)((float.Parse(textBox6.Text)) / 100);
            float scaleY = (float)((float.Parse(textBox7.Text)) / 100);

            int sourceWidth = bmp.Width;
            int sourceHeight = bmp.Height;
            int sourceX = 0;
            int sourceY = 0;

            int destX = 0;
            int destY = 0;
            int destWidth = (int)(sourceWidth * scaleX);
            int destHeight = (int)(sourceHeight * scaleY);

            pictureBox1.Width = destWidth;
            pictureBox1.Height = destHeight;

            Bitmap bmPhoto = new Bitmap(destWidth, destHeight,
                                     PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(bmp.HorizontalResolution,
                                    bmp.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(bmp,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            bmPhoto.Dispose();

-AJ

Share this post


Link to post
Share on other sites
Advertisement
Maybe I'm missing something, but shouldn't this:

Graphics grPhoto = Graphics.FromImage(bmPhoto);

be this:

Graphics grPhoto = picureBox1.CreateGraphics();

You're creating a new bitmap and drawing an image on it, but you're not displaying the bitmap anywhere that I can see.

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
Maybe I'm missing something, but shouldn't this:

Graphics grPhoto = Graphics.FromImage(bmPhoto);

be this:

Graphics grPhoto = picureBox1.CreateGraphics();

You're creating a new bitmap and drawing an image on it, but you're not displaying the bitmap anywhere that I can see.


he would still need the bitmap because painting directly to a control causes flickering, the bitmap is a back buffer.

however, there is really no point to using a picturebox, or any control other than the form that you are displaying in, unless you specifically do not want to use the entire form, in which case you should use a panel.

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
Quote:
Original post by Machaira
Maybe I'm missing something, but shouldn't this:

Graphics grPhoto = Graphics.FromImage(bmPhoto);

be this:

Graphics grPhoto = picureBox1.CreateGraphics();

You're creating a new bitmap and drawing an image on it, but you're not displaying the bitmap anywhere that I can see.


he would still need the bitmap because painting directly to a control causes flickering, the bitmap is a back buffer.

Only if you're updating the picture more than once [grin] but you are correct sir.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!