473,410 Members | 1,937 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,410 software developers and data experts.

Image not Displaying


Hi

why does the following code not display the image

public Form1()
{
InitializeComponent();

Bitmap imgBg = new Bitmap("Monitor.bmp");
Graphics gr = Graphics.FromImage(imgBg);
gr.DrawImage(imgBg, new Rectangle(10,10, imgBg.Width, imgBg.Height));
}

if i shift the above code in the paint event like this it displays the
bitmap

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{

Bitmap imgBg = new Bitmap("Monitor.bmp");
e.Graphics.DrawImage(imgBg, new Rectangle(10,10, imgBg.Width,
imgBg.Height));
}
Barry

Nov 17 '05 #1
2 1415
Barry,

The Graphics object does not retain any information about what is actually drawn. You need to redraw the image whenever needed and since Paint is called after the constructor the image will be wiped before you get to see it.

You should and you have put it inside the Paint procedure as this is where you should do all drawing. However, create the bitmap once in the constructor, store the reference and let Paint use that reference when drawing. Creating the bitmap each time Paint is called may slow down the drawing.

Take a look at the GDI+ Faq for more information regarding Paint and drawing (see question #1)

http://www.bobpowell.net/faqmain.htm
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
In the first instance you're trying to draw an image onto itself. This won't
work. In the second you're drawing it onto the form's surface in the correct
manner.

You probably need to be reading the GDI+ FAQ to retain your sanitiy.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Barry" <so*****@somwhere.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

Hi

why does the following code not display the image

public Form1()
{
InitializeComponent();

Bitmap imgBg = new Bitmap("Monitor.bmp");
Graphics gr = Graphics.FromImage(imgBg);
gr.DrawImage(imgBg, new Rectangle(10,10, imgBg.Width, imgBg.Height));
}

if i shift the above code in the paint event like this it displays the
bitmap

private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{

Bitmap imgBg = new Bitmap("Monitor.bmp");
e.Graphics.DrawImage(imgBg, new Rectangle(10,10, imgBg.Width,
imgBg.Height));
}
Barry

Nov 17 '05 #3

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

Similar topics

2
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
5
by: ken | last post by:
I had to reinstall Windows 2000 Pro and of course Access 2000. I HAD each record displaying a photo but now it does not show them. The WEIRD thing is that when I upload the database to my...
2
by: Al Reid | last post by:
Is it possible to display an image that is stored on the server as a TIFF image, on an ASP.Net page without the use of an add-in viewer? If so, could someone tell me how to do it? TIA -- Al...
2
by: pmud | last post by:
Hi, I am designing a simple ASP.NET web page. I have an image button. What I want to do is: when the cursor is over the image button, a drop down meny should display. This menu should have tabs...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
3
by: CD | last post by:
An application is logging faxes sent in SQL2000 image column type. I have found code on the net but what it is doing is prompting to save to local which is fine for single page image. Not good...
5
by: ljuljacka | last post by:
I'm trying to display resized images. Locations of images are fetched from database. The problem is that with the following code, I get only the first image displayed: <?php...
6
by: Jeff | last post by:
hi asp.net 2.0 I have a image (.jpeg) stored in sql server 2005 and now I want to display it on a webpage. So I created a webpage (Image.aspx) which just writes the buffer data to the...
3
by: brijesh1234 | last post by:
dear Sir/madam I am getting a problem while displaying a image from database Database : MSSQL2000 I have a table named “Image” having following fields as ImageId int Image Image
3
by: mvijayrkumar | last post by:
Hi to all.... Guys pls help me..... I have an image issue in RLDC while hosting my project on server.The image displays or works fine with the local system.But when hosted on server,both the...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.