Click here to Skip to main content
13,199,680 members (53,827 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I need to write this sample code so that the resulting image is the exact same as the original.

Dim a = New Bitmap( _
 My.Application.Info.DirectoryPath & "\a.png")
 Dim b As New Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb )
 Dim g As Graphics = Graphics.FromImage(b)
 g.DrawImage(a, b.GetBounds(GraphicsUnit.Pixel))
 PictureBox1.Image = b
 b.Save("b.png", System.Drawing.Imaging.ImageFormat.Png)

I don't understand what happens when I convert an image to graphics. Image.FromFile preserves all of the picture data, but graphics changes the picture size so that g.DrawImage(a, 0, 0) would be a larger picture, but cropped to the original's size. Graphics.FromImage resizes the picture and so some quality is lost.

Can this be rewritten so that the result is the same as the original (no resizing/loss of quality)?
Posted 15-Jan-13 3:14am
Updated 15-Jan-13 3:22am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You're going through a lot of code to do something very simple.

Dim b As New Bitmap(a)

Try reading the documentation on the Bitmap class and look at the overloads for the constructor.
kamikazehighland 15-Jan-13 10:46am
Thanks, but there's still loss of quality. I guess I just don't get why the result changes the colors.
Dave Kreskowiak 15-Jan-13 12:45pm
There are two possibilities. Either the original image is screwed up or the control you're using to display the image is scaling up the image, making it blurry. Don't set the PictureBox control to stretch the image (enlarge it to fit the control) and it won't look bad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100