Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# winforms: graphics.DrawImage issue

Tags:

c#

winforms

gdi+

I have a really strange problem with Graphics.DrawImage method.

I have the PictureBox control in the Panel control with AllowScroll property = true. The program cuts the image on small parts basing on the area selected by the user.

I load the image 300x547 and select the area (the red rectangle):

alt text

program properly cuts the image:

alt text

then, I load another image 427x640:

alt text http://img34.imageshack.us/img34/7950/56727000.png

and then, as the result I see that the image is not cut properly. Each img.jpg file has properly width & height but the drawn image is too small: alt text

here's the code snippet - it saves the bitmap area selected by the user:

  Image OriginalIMG= (Image)((PictureBox)panel1.Controls["picBox"]).Image.Clone()
  Bitmap bmp = new Bitmap(selectedAreaRECT.Width, selectedAreaRECT.Height);
  Graphics g = Graphics.FromImage(bmp);

  g.DrawImage(OriginalIMG, 0,0, selectedAreaRECT, GraphicsUnit.Pixel);
  g.Save();
  g.Dispose();

  bmp.Save(AppDomain.CurrentDomain.BaseDirectory + @"\Temp\" + "img1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

As You see, the code is the same for the img1.jpg from image A and from Image B. I'm trying to resolve that stupid problem for too long, I don't know what's the reason of that problem. I tried different overloads of the DrawImage method, with no success

EDIT

Resolved! the dafault DPI value of the System.Drawing.Bitmap is = 96, if I open an image with DPI != 96 then the problem described above occurs. To get rid of it, I needed to use SetResolution method:

Bitmap result = new Bitmap(width, height);
result.SetResolution(OriginalIMG.HorizontalResolution, OriginalIMG.VerticalResolution);

that resolves the problem :) Thanks for everyone for help ! :)

like image 868
Tony Avatar asked Apr 21 '10 09:04

Tony


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

What == means in C?

The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false .

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

I'd try: (edited)

  g.DrawImage(
    OriginalIMG,
    new Rectangle( Point.Empty, bmp.Size ),
    selectedAreaRECT.X, selectedAreaRECT.Y,
    selectedAreaRECT.Width, selectedAreaRECT.Height, 
    GraphicsUnit.Pixel);

to see if it makes a difference.

Although it has nothing to do with your problem: you're forgetting to .Dispose() some things, and I'm not sure why you have to .Clone() the image.

like image 125
Dan Byström Avatar answered Oct 20 '22 02:10

Dan Byström