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?
---------
Thanks
Sharon