Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VB.NET
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)
 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 (no resizing/loss of quality)?
Posted 15-Jan-13 4:14am
Edited 15-Jan-13 4:22am
v2

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
kamikazehighland at 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 at 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
0 Mathew Soji 274
1 BillWoodruff 270
2 Afzaal Ahmad Zeeshan 244
3 Sergey Alexandrovich Kryukov 240
4 Raul Iloc 160
0 OriginalGriff 6,219
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,103
3 Manas Bhardwaj 4,549
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
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