Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VBVB.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 3:14am
Edited 15-Jan-13 3: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
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 400
1 Shai Vashdi 334
2 Sergey Alexandrovich Kryukov 305
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 8,979
1 OriginalGriff 5,280
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid