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 (834457)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0

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.

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));

STATUS

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

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.

Modification Type:MajorLast Reviewed:12/14/2004
Keywords:kbWindowsForms kbGDI kbtshoot KB834457 kbAudDeveloper kbAudITPRO