PC Review


Reply
Thread Tools Rate Thread

Bitmap to PictureBox control problem

 
 
=?Utf-8?B?U2hhcm9u?=
Guest
Posts: n/a
 
      21st Sep 2005
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
 
Reply With Quote
 
 
 
 
Andrew Kirillov
Guest
Posts: n/a
 
      21st Sep 2005
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)

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072


"Sharon" <(E-Mail Removed)> wrote in message
news:39869334-E24F-4979-8A0F-(E-Mail Removed)...
>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



 
Reply With Quote
 
=?Utf-8?B?U2hhcm9u?=
Guest
Posts: n/a
 
      22nd Sep 2005
Surprisingly for me it works as you said.


Thanks a lot
Sharon
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the PictureBox downsampling for its bitmap? Sharon Microsoft C# .NET 0 16th Oct 2008 01:36 PM
Scaling a bitmap in a picturebox Eric Microsoft Dot NET 1 7th Aug 2006 10:14 AM
Scaling a bitmap in a picturebox Eric Microsoft VB .NET 2 7th Aug 2006 10:14 AM
copy a portion of a bitmap to another bitmap (via picturebox or other method) ? Mad Scientist Jr Microsoft VB .NET 2 31st Jan 2005 02:26 PM
How to Zoom on bitmap in PictureBox Anders Fredborg Microsoft Dot NET Compact Framework 1 2nd Jun 2004 11:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:15 AM.