Bitmap to PictureBox control problem

G

Guest

I encountered a strange behavior when doing ‘new Bitmap’:

The following code works fine and the given bitmap file is shown on the
PictureBox (the m_DrawArea) in the correct bitmap sizes:

private Graphics m_gMapImg;
private Bitmap m_backroundImage;
private Bitmap MapImg;
private PictureBox m_DrawArea;

protected void OnResize(object sender, System.EventArgs e)
{
if( m_MapImg != null )
{
m_MapImg.Dispose();
m_gMapImg.Dispose();
}
if( m_DrawArea.Width != 0 && m_DrawArea.Height != 0 )
{
m_MapImg = new Bitmap(m_DrawArea.Width, m_DrawArea.Height);
m_gMapImg = Graphics.FromImage(m_MapImg);
}
}

protected override void OnPaint(PaintEventArgs e)
{
m_gMapImg.Clear(Color.Transparent);
m_gMapImg.DrawImage(m_backroundImage, 0, 0);

m_DrawArea.Image = m_MapImg;

base.OnPaint(e);
}

public Bitmap BackImage
{
set {
if( value != null )
m_backroundImage = new Bitmap(value);
}
}

public string BackgroundImagePath
{
set {
if( System.IO.File.Exists(value) )
BackImage = new Bitmap(value);
}
}


But as you can see, the ‘new Bitmap’ is done twice when the
BackgroundImagePath property is called. And the BackImage property is
creating a new bitmap out of the given bitmap.

So I changed the code of the BackImage to:
public Bitmap BackImage
{
set {
if( value != null )
m_backroundImage = value;
}
}

And now the bitmap file that is given to me by the BackgroundImagePath
property is shown on the PictureBox (the DrawArea) control as a thumbnail and
not in his original size.

The same problem occurs if only change the BackgroundImagePath property to:
public string BackgroundImagePath
{
set {
if( System.IO.File.Exists(value) )
m_backroundImage = new Bitmap(value);
}
}

I can’t make sense of it.
Can anybody tell me why or how it acts so strangely?
 
A

Andrew Kirillov

Hello,

m_gMapImg.DrawImage(m_backroundImage, 0, 0) will draw the specified image,
using its original physical size according to it's DPI and screen
resolution.

Here is a note from MSDN:
"The physical width, measured in inches, of an image is the pixel width
divided by the horizontal resolution. For example, an image with a pixel
width of 216 and a horizontal resolution of 72 dots per inch has a physical
width of 3 inches. Similar remarks apply to pixel height and physical
height."

So, if you want to draw image using it's width and height in pixels, you
can:
m_gMapImg.DrawImage(m_backroundImage, 0, 0, m_backroundImage.Width,
m_backroundImage.Height)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Bitmap[] property 2
Extract From Bitmap Band/Strip 6
Unexpected Stripes In Bitmap 2
PictureBox Zoom Problem 4
Printing tiny controls, when painting 0
Animation in Winform 4
About bitmap 1
AlphaBlend in CF 4

Top