473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Bitmap to PictureBox control problem

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
Nov 17 '05 #1
2 2988
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" <Sh*****@newsgroups.nospam> wrote in message
news:39**********************************@microsof t.com...
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

Nov 17 '05 #2
Surprisingly for me it works as you said.
Thanks a lot
Sharon
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jose Michael Meo R. Barrido | last post by:
Hi! Can u please tech me a way to know if the image in the picturebox has changed? what event should i catch the chage?
5
by: Sharon | last post by:
I’m writing a Windows application. In the form I have a Panel and inside the panel I have a PictureBox control. I’m loading the PictureBox control with BMP image that has the following...
2
by: David Ricker | last post by:
I have created a PictureBox control which can have it's Image property directly bound to an image field in a database. This works perfectly for showing the images that are in the database. When I...
3
by: Tyson Ackland | last post by:
Can someone tell me how you go about handling events where your window might be obscured and then when you bring it to the foreground, you want your picturebox control to be repainted instead of...
5
by: BrianW | last post by:
I am working on a program that has multiple picturebox controls that a user is allowed to move around which are contained within a panel control for visual placement. In my mousedown event, I set...
3
by: ktcute | last post by:
I'm trying to write a simple drawing tool that saves images out as jpgs. I've been using the paint event on the Panel control to draw the objects, however I couldn't get the same drawing...
14
by: GSSI | last post by:
I have an image data field in a datarow (as in the Northwinds demo sql database). What is the C# instruction syntax to display it in a Winform picturebox control. -- GSSI
3
by: prynhart | last post by:
I have a PictureBox Control which is 96*96 pixels. I want to display nine 32*32 pixel bitmaps in this control arranged in a 3X3 square. How can I do this ? All the examples I've seen load only one...
10
Samishii23
by: Samishii23 | last post by:
Ok so, I'm trying to make an .exe version of a World of Warcraft system. The Talent builder. I'm hoping that no one points and laughs too much... But anyways, this is the problem I'm having... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.