Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi what's the gap between two rectangles in the following code?
C#
void f5(Graphics g)
{
    var img = Bitmap.FromFile(@"d:\small2.png");
    var destRect = new Rectangle(0, 0, 132, 60);
    var srcRect = new Rectangle(0, 0, 60, 60);
    g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
    destRect.X += destRect.Width;
    g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    f5(e.Graphics);
}

d:\small2.jpg is a black 60x60 box.
see the result:
http://www.uploadup.com/di-YN9F.png[^]
the gap under the microscope:
http://www.uploadup.com/di-VJA4.png[^]
Posted
Updated 10-Apr-12 4:23am
v3
Comments
Ed Nutting 10-Apr-12 11:14am    
Please clarify your question - what are you actually trying to calculate exactly?

Ed
ilostmyid2 10-Apr-12 12:07pm    
ok, let me describe it with another example which contrasts the problem. for regenerating it, please go to Paint and create a 2x2 image and fill it with the black color. then save it as d:\small3.png. now you have a 2x2 image which its entire surface is black. then create a new C# form application in which a picture box inserted with no margin. then use the following code:
void f6(Graphics g)
{
var img = Image.FromFile(@"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 400;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
f6(e.Graphics);
}
i expect the entire rectangle inside the blue margins be black while the output is as follows:
http://www.uploadup.com/di-L034.png
why?!
i hope i could describe the problem well.
thx
ilostmyid2 11-Apr-12 2:57am    
oh! i found that it depends on the color of the remainder part of the picture box! for example if i add the following code before DrawRectangle:
g.FillRectangle(new SolidBrush(Color.DarkCyan), pictureBox1.ClientRectangle);
the gradient effect which DrawImage causes gets between black to dark cyan!
i also change the srcRect from (0, 0, 2, 2) to (0, 0, 1, 1) and this time DrawImage doesn't produce the gradient effect.
someone describe please :(
ilostmyid2 11-Apr-12 4:56am    
oh! i surrender understanding the behavior of DrawImage!
see what happened! with the 2x2 image changed to:
http://www.uploadup.com/di-WCGA.png
and the following code:
void f8(Graphics g)
{
var img = Image.FromFile(@"d:\small3.png");
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 50;
int w = img.Width*factor;
g.FillRectangle(new SolidBrush(Color.DarkCyan), pictureBox1.ClientRectangle);
var destRect = new Rectangle(0, 0, 3 * w, w); // assuming img.Width == img.Height
g.FillRectangle(new SolidBrush(Color.Chartreuse), destRect);
destRect.Y = 2*w;
g.FillRectangle(new SolidBrush(Color.Chartreuse), destRect);
destRect.X = 0;
destRect.Y = w;
destRect.Width = w;
g.FillRectangle(new SolidBrush(Color.Chartreuse), destRect);
destRect.X = 2*w;
g.FillRectangle(new SolidBrush(Color.Chartreuse), destRect);
destRect.X = w;
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}
i get the following result:
http://www.uploadup.com/di-OMVI.png
it's weird! consider drawing the image after drawing the margin with color Chartreuse.
ilostmyid2 11-Apr-12 5:52am    
and the following code:
void f9(Graphics g)
{
var img = new Bitmap(3, 2);
var memG = Graphics.FromImage(img);
memG.FillRectangle(new SolidBrush(Color.Black), 0, 0, img.Width, img.Height);
GlobalFunctions.PutPixel(memG, new Pen(Color.Red), 1, 0);
GlobalFunctions.PutPixel(memG, new Pen(Color.Blue), 0, 1);
GlobalFunctions.PutPixel(memG, new Pen(Color.GreenYellow), 2, 0);
GlobalFunctions.PutPixel(memG, new Pen(Color.Cyan), 2, 1);
var srcRect = new Rectangle(0, 0, img.Width, img.Height);
int factor = 100;
var destRect = new Rectangle(0, 0, img.Width * factor, img.Height * factor);
g.FillRectangle(new SolidBrush(Color.DarkCyan), pictureBox1.ClientRectangle);
g.DrawRectangle(new Pen(Color.Blue), destRect);
g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
}
generates the following result:
http://www.uploadup.com/di-0B33.png
no description?
indeed i expect this to be the result of drawing an image which is 4x3 instead of 3x2 which its 3 right points and 4 bottom points be of color dark blue. got what i mean?

1 solution

Hi - do you have that result on different machines (or using different displays /graphic cards / graphic drivers)?
Tried your first example - no gap...
regards

Tried the 2nd too - not such a result...
regards
 
Share this answer
 
v2
Comments
ilostmyid2 10-Apr-12 14:54pm    
really?! what about the second one?
no, i've not. but i will.
it's weird that we may get different results on different machines!
it must be standard in all machines!
ilostmyid2 10-Apr-12 15:06pm    
i tried another computer, this time a netbook, but with the same OS (Windows 7 Ultimate). it produced the same result. i think all must produce the same result and it must not be system/OS dependent. it depends on GDI+ and it's the same in all machines, i think.
bejituharo 11-Apr-12 18:02pm    
I used WinXP, and I don't know if it uses the same (identical) GDI+ - implementation as Win7...
there is an overloaded DrawImage() with additional param ImageAttributes, responsible for some changes how the image is drawn...
regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900