I try to do something like off-screen drawing. First, I create a bitmap, draw on the bitmap, then use the BitBlt() function to draw it on the panel surface.
however, all I got is a rectangle with black color. There might be the way calling BitBlt was not so correct. Can someone help me out?
thanks a lot.
Alex
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Runtime.InteropServices;
using System.Timers;
namespace TransparentBitmap
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private MyPanel panel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
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.panel = new TransparentBitmap.MyPanel();
this.SuspendLayout();
//
// panel
//
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(300, 300);
this.panel.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(368, 328);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.panel.StartTimer();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Form1 form = new Form1();
Application.Run(form);
}
}
//----------------------------------------------------------------//
public class MyPanel : System.Windows.Forms.UserControl
{
private System.Timers.Timer timer;
Bitmap myBitmap ;
Graphics bitmapGraphics;
IntPtr bitmapPtr;
public MyPanel()
{
DrawBitmap();
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.UserPaint, true);
}
[DllImport("gdi32.dll")]
public static extern bool BitBlt(
IntPtr hdcDest,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc,
int nXSrc,
int nYSrc,
long dwRop
);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(
IntPtr hwnd
);
public void redraw(Graphics g)
{
if( this.IsDisposed )
{
return;
}
if( ! g.IsVisibleClipEmpty )
{
IntPtr hdcScreen = g.GetHdc();
bitmapPtr = bitmapGraphics.GetHdc();
BitBlt(hdcScreen, 0, 0, 300, 300, bitmapPtr,
0, 0,
0x00CC0020);
g.ReleaseHdc(hdcScreen);
bitmapGraphics.ReleaseHdc(bitmapPtr);
}
}
protected override void OnPaint(PaintEventArgs e)
{
redraw(e.Graphics);
}
protected virtual void TimerOnTick( object obj, ElapsedEventArgs ea)//EventArgs ea)
{
this.Refresh();
}
public void StartTimer()
{
timer = new System.Timers.Timer();
timer.Interval = 20;
timer.Elapsed+=new ElapsedEventHandler(TimerOnTick);
timer.Enabled = true;
}
public void DrawBitmap()
{
myBitmap = new Bitmap(300,300);
bitmapGraphics = Graphics.FromImage(myBitmap);
bitmapGraphics.FillRectangle(Brushes.Red,0,0,300,100);
bitmapGraphics.FillRectangle(Brushes.Yellow,0,100,300,100);
bitmapGraphics.FillRectangle(Brushes.Blue,0,200,300,100);
}
}
}
however, all I got is a rectangle with black color. There might be the way calling BitBlt was not so correct. Can someone help me out?
thanks a lot.
Alex
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Runtime.InteropServices;
using System.Timers;
namespace TransparentBitmap
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private MyPanel panel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
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.panel = new TransparentBitmap.MyPanel();
this.SuspendLayout();
//
// panel
//
this.panel.Name = "panel";
this.panel.Size = new System.Drawing.Size(300, 300);
this.panel.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(368, 328);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.panel});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.panel.StartTimer();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Form1 form = new Form1();
Application.Run(form);
}
}
//----------------------------------------------------------------//
public class MyPanel : System.Windows.Forms.UserControl
{
private System.Timers.Timer timer;
Bitmap myBitmap ;
Graphics bitmapGraphics;
IntPtr bitmapPtr;
public MyPanel()
{
DrawBitmap();
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.UserPaint, true);
}
[DllImport("gdi32.dll")]
public static extern bool BitBlt(
IntPtr hdcDest,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc,
int nXSrc,
int nYSrc,
long dwRop
);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(
IntPtr hwnd
);
public void redraw(Graphics g)
{
if( this.IsDisposed )
{
return;
}
if( ! g.IsVisibleClipEmpty )
{
IntPtr hdcScreen = g.GetHdc();
bitmapPtr = bitmapGraphics.GetHdc();
BitBlt(hdcScreen, 0, 0, 300, 300, bitmapPtr,
0, 0,
0x00CC0020);
g.ReleaseHdc(hdcScreen);
bitmapGraphics.ReleaseHdc(bitmapPtr);
}
}
protected override void OnPaint(PaintEventArgs e)
{
redraw(e.Graphics);
}
protected virtual void TimerOnTick( object obj, ElapsedEventArgs ea)//EventArgs ea)
{
this.Refresh();
}
public void StartTimer()
{
timer = new System.Timers.Timer();
timer.Interval = 20;
timer.Elapsed+=new ElapsedEventHandler(TimerOnTick);
timer.Enabled = true;
}
public void DrawBitmap()
{
myBitmap = new Bitmap(300,300);
bitmapGraphics = Graphics.FromImage(myBitmap);
bitmapGraphics.FillRectangle(Brushes.Red,0,0,300,100);
bitmapGraphics.FillRectangle(Brushes.Yellow,0,100,300,100);
bitmapGraphics.FillRectangle(Brushes.Blue,0,200,300,100);
}
}
}