Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 834457 - Last Review: December 14, 2004 - Revision: 1.1
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
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.
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));
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.
Steps to reproduce the behavior
- Start Microsoft Visual Studio .NET, and then create a Microsoft Windows application by using Microsoft Visual C# .NET.
- 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());
}
}
}
- Modify the code accordingly where you see the TODO comment.
- Press CTRL+F5 to run the application.
You see the result that is described in the "Symptoms" section.
APPLIES TO
- Microsoft .NET Framework 1.1
- Microsoft .NET Framework 1.0
| kbtshoot kbwindowsforms kbgdi KB834457 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate