473,749 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Graphics.FromIm age and DrawImage

1 New Member
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.FromIm age 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
1 3781
IronRazer
83 New Member
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

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

Similar topics

8
12276
by: Mark Johnson | last post by:
Using: VS 2003 NET C# for Framework and Framework Compact Trying : Moving a Card (Bitmap) as in Solitare (PC + WinCe) Version on OnMouseMove Problem : The affected drawing Area by Invalidate (or Invalidate(Rectangle)) flickers in a nasty way when repainting. This does not happen in the Solitare (PC + WinCe) Versions a well as in a Card game where I have the C++ Source. The use of an empty OnPaintBackground brings no visable results.
1
10138
by: Ivan | last post by:
Hi there I'm trying to use BitBlt function instead od DrawImage beacuse I need speed in my app. I have one Bitmap (bmp) that is loaded from file and I use it in OnPaint for drawing. Currently I use this code, but it does not work (it draws only black). What am I missing ?! public int LoadImage (string strFileName) { // load bitmap
1
2057
by: Ajay | last post by:
Hello Guys, Iam working on a zoom in and out tool and here is the code iam using for it //Method for zooming in Bitmap bmpSource=new Bitmap(ImgBox.Image); Bitmap bmpZoomed=new Bitmap(bmpSource.Width*ZoomInFactor,bmpSource.Height*ZoomInFactor); Graphics grZoom=Graphics.FromImage(bmpZoomed);
4
1799
by: Darren Clark | last post by:
I am trying to simply embed a opaque image over another image... if i use a windows form it works.. e.g. Graphics g = this.pictureBox1.CreateGraphics(); g.Clear(this.BackColor);
0
1178
by: Steven | last post by:
I need to draw to an image after it has been rotated, but if I draw something before the rotation is done then DrawImage doesn't work: Here's the code. using System; using System.Drawing; class ImageTest {
2
15184
by: yotaxp | last post by:
Hello, all. This line has been pestering me, and I can't seem to figure out why the problem is occuring. I'm trying to paint a GraphicsPath onto a Bitmap, using the Graphics class, but it always seems to crash, and throw the "Out of Memory" exception when it comes to the point I need the create the object. I've been fairly strict with IDisposable.Dispose(), and the 'using' as well. It always seems to be at this exact line, when the...
0
1207
by: Marcelo | last post by:
Greetings! I am developping a multiple forms Windows Forms application in Visual Studio C++ .NET 2003 and I need to make graphics in a group box. In order to do this I use: void InitializeComponent(void){ this->groupBox1->Paint += new System::Windows::Forms::PaintEventHandler(this, groupBox1_Paint); }
2
1676
by: Tamer Abdalla via DotNetMonster.com | last post by:
Hello, everyone! I DO need some help in order to understand how to create graphics in VB.NET. I'm a little bit confused... I once knew a time when using Point & PSet was almost the only way to make some interesting apps which could tranform images (i.e. making saturation of colours "heavy", or gradually fade to grayscale, or "erasing" a colour... and so on), while nowadays it seems quite impossible. Now that I got .NET over...
3
1481
by: news.microsoft.com | last post by:
Hello, can I do the opposite of: Dim newGraphics As Graphics = Graphics.FromImage(imageFile) I want an image from a graphics. thx.
9
2662
by: DaveL | last post by:
hello I have a Bit map 1367 wide 32 high this bitmap contains like 40 separate Images 32x32 I tell it the id*32 to get the approiate Image from the source Bitmap When i CreateGraphics() From the Standard CreateGraphics() function the code below works
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.