Help and Support
 

powered byLive Search

BUG: You cannot use the Graphics.DrawXxxx methods to draw on a surface that is created by using the Graphics.FromImage method in the .NET Framework 1.0 or the .NET Framework 1.1

Article ID:834457
Last Review:December 14, 2004
Revision:1.1
On This Page

SYMPTOMS

The Graphics.DrawXxxx methods in the Microsoft .NET Framework do not draw when you do the following:
You create a Graphics object by using the Graphics.FromImage method on a loaded bitmap.
Then you use the Graphics.DrawXxxx methods to draw on top of the bitmap.
The bitmap is also reloaded.

Back to the top

WORKAROUND

To work around this problem, use the Bitmap.MakeTransparent method. To do this, locate the following line of code in step 2 of the "Steps to reproduce the behavior section":
Bitmap b = new Bitmap(BitmapPath);
Under this line of code, add the following code:
b.MakeTransparent(Color.FromArgb(0,0,0));

Back to the top

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

Back to the top

MORE INFORMATION

Steps to reproduce the behavior

1.Start Microsoft Visual Studio .NET, and then create a Microsoft Windows application by using Microsoft Visual C# .NET.
2. Replace the code in Form1.cs with the following code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication3
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			InitializeComponent();

			//TODO: Replace BitmapPath with the path of your bitmap.
			Bitmap b = new Bitmap(BitmapPath);
			Graphics gb = Graphics.FromImage(b);

			Rectangle rBounds = new Rectangle( 0, 0, b.Width, b.Height);
			gb.DrawRectangle(Pens.Purple, rBounds); 
			gb.DrawEllipse(Pens.Yellow, rBounds); 
			gb.DrawLine(Pens.Red, 0, 0, b.Width, b.Height);
			gb.Dispose();

			Point p = new Point(0,0);

			this.ClientSize = b.Size;
			Graphics g = this.CreateGraphics();
			this.Show();
			g.DrawImage(b, p); 
			g.Dispose();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.Size = new System.Drawing.Size(300,300);
			this.Text = "Form1";
		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	}
}
3.Modify the code accordingly where you see the TODO comment.
4.Press CTRL+F5 to run the application.
You see the result that is described in the "Symptoms" section.

Back to the top


APPLIES TO
Microsoft .NET Framework 1.1
Microsoft .NET Framework 1.0

Back to the top

Keywords: 
kbtshoot kbwindowsforms kbgdi KB834457

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by Email, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.