4 Replies - 2436 Views - Last Post: 04 February 2010 - 03:10 AM Rate Topic: -----

#1 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Image is not displayed correctly

Posted 01 February 2010 - 02:13 PM

Hi,

I have been trying to experiment resizing an image from user input so i set up a click event_handler for a button but everytime i try to resize an image the image in the picturebox dissapears. Could someone assist me please with this problem?

Here is the code behind the click event handler for a button.

Image img = pictureBox1.Image;
Image resizedImage = ScaleByPercent(img, 50);
pictureBox1.Image = resizedImage;



When i debugged the code i noticed that the size of the rescaled image would change but its not displayed correctly in the picturebox.

The code below is used to scale the image
static Image ScaleByPercent(Image Photo, int Percent)
{
	float nPercent = ((float)Percent/100);

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

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

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

	Graphics graphics = Graphics.FromImage(bitmap);
	graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

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

	grPhoto.Dispose();
	return bmPhoto;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Image is not displayed correctly

#2 jbbatts77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-January 09

Re: Image is not displayed correctly

Posted 02 February 2010 - 01:47 PM

View PostMarmiteX1, on 01 February 2010 - 01:13 PM, said:

Hi,

I have been trying to experiment resizing an image from user input so i set up a click event_handler for a button but everytime i try to resize an image the image in the picturebox dissapears. Could someone assist me please with this problem?

Here is the code behind the click event handler for a button.

<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->
Image img = pictureBox1.Image;
Image resizedImage = ScaleByPercent(img, 50);
pictureBox1.Image = resizedImage;
<!--c2--></div><!--ec2-->

When i debugged the code i noticed that the size of the rescaled image would change but its not displayed correctly in the picturebox.

The code below is used to scale the image
<!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->
static Image ScaleByPercent(Image Photo, int Percent)
{
float nPercent = ((float)Percent/100);

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

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

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

Graphics graphics = Graphics.FromImage(bitmap);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

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

grPhoto.Dispose();
return bmPhoto;
}
<!--c2--></div><!--ec2-->



it looks like you just need to change the size mode property of your picturebox to autosize.

pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;


Was This Post Helpful? 0
  • +
  • -

#3 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Image is not displayed correctly

Posted 02 February 2010 - 03:54 PM

Quote

it looks like you just need to change the size mode property of your picturebox to autosize.

pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;



Hi thanks,

I tried to do this but the image is still not being displayed eventhough the window of the form adjusts automatically as i set the Autosize mode to "Grow and shrink".

Any advice?

Thanks,
Was This Post Helpful? 0
  • +
  • -

#4 jbbatts77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 07-January 09

Re: Image is not displayed correctly

Posted 03 February 2010 - 06:52 AM

sorry about that, i should have taken a better look.

you have to draw the graphic from you original image not the newly created one. i tweaked your code a little bit and it worked fine on.

static Image ScaleByPercent(Image Photo, int Percent)
        {
            float nPercent = ((float)Percent / 100);

            int sourceWidth = Photo.Width;
            int sourceHeight = Photo.Height;

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

            Bitmap bitmap = new Bitmap(destWidth, destHeight);
            Graphics graphics = Graphics.FromImage((Image)bitmap);

            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(Photo, 0, 0, destWidth, destHeight);
            graphics.Dispose();

            return (Image)bitmap;
        }


Was This Post Helpful? 0
  • +
  • -

#5 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Image is not displayed correctly

Posted 04 February 2010 - 03:10 AM

View Postjbbatts77, on 03 February 2010 - 04:52 AM, said:

sorry about that, i should have taken a better look.

you have to draw the graphic from you original image not the newly created one. i tweaked your code a little bit and it worked fine on.


No problem, thanks for your help. Just out of interest why does the method have a problem when the picturebox.size is set to "zoom"?

The reason being is that I would like my images to be viewable at 1024 x 768 so if the width and height of an image is say 500 x 600, i want it to be displayed fully at 1024 x 768 so that it fills up the the entire picturebox area if possible and maintaining the aspect ratio?

I tried to create a new Point and set the picturebox width and height based on whether the width and height of the point but the image is not filling up the entire picturebox area.

Any advice please?

Thanks,

This post has been edited by MarmiteX1: 04 February 2010 - 05:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1