By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,641 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,641 IT Pros & Developers. It's quick & easy.

Graphics.FromImage and DrawImage

P: 1
I need to write this sample code so that the resulting image is the exact same as the original.

Expand|Select|Wrap|Line Numbers
  1.             Dim a = New Bitmap( _
  2.     My.Application.Info.DirectoryPath & "\a.png")
  3.             Dim b As New Bitmap(a.Width, a.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  4.             Dim g As Graphics = Graphics.FromImage(b)
  5.             g.DrawImage(a, b.GetBounds(GraphicsUnit.Pixel))
  6.             PictureBox1.Image = b
  7.             b.Save("b.png", System.Drawing.Imaging.ImageFormat.Png)
  8.             g.Dispose()
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?

Edit: I need to get the above to work, but
Expand|Select|Wrap|Line Numbers
  1.  Dim b As New Bitmap(a) 
also results in a similar loss of quality.
Jan 15 '13 #1
Share this Question
Share on Google+
1 Reply


IronRazer
P: 83
Hello kamikazehigh,
I have looked at the code and even tested it but i was unable to produce the results you talked about. I do notice that the b.png is slightly different as the png i tested was 82.1 kb in size and the output was 86 kb but, I did not see a difference in height or width of b.png. If you mean that the image in the picturebox is clipped then try setting the size mode property of the picturebox to zoom. If you don`t need to use the a, b, and g variables for any other purpose then you could simply use the following which produces a very accurate copy of the picture :

Expand|Select|Wrap|Line Numbers
  1.         PictureBox1.ImageLocation = My.Application.Info.DirectoryPath & "\a.png"
  2.         PictureBox1.Image.Save(My.Application.Info.DirectoryPath & "\b.png", System.Drawing.Imaging.ImageFormat.Png)
  3.  
Jan 19 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.