C# 自己写的一个图像显示控件,怎么调用后什么也不显示。调试的时候DrawAll()是执行了的
public partial class Map : UserControl
{
public Map()
{
InitializeComponent();
}
private object _lckDrawing = new object();
private Bitmap _backgroundImage;
private Rectangle _backgroundRect;
public void DrawAll(Graphics bufferedCanvas)
{
bufferedCanvas.Clear(Color.Black);
bufferedCanvas.SmoothingMode = SmoothingMode.HighQuality;
bufferedCanvas.CompositingQuality = CompositingQuality.AssumeLinear;
bufferedCanvas.InterpolationMode = InterpolationMode.Low;
bufferedCanvas.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
bufferedCanvas.DrawEllipse(Pens.White, 0, 0, 30, 21);
GetMapObject gt=new GetMapObject();
lock (_lckDrawing)
{
try
{
foreach (Lane lane in gt.GetLanes())
{
bufferedCanvas.DrawLine(Pens.White, lane.Point1, lane.Point2);
}
}
catch (System.Runtime.InteropServices.ExternalException)
{
}
}
zoomTrackBar1.DrawAll(bufferedCanvas);
}
public override void Refresh()
{
RePaint();
this.Invalidate();
this.Update();
}
protected override void OnResize(EventArgs e)
{
Refresh();
}
protected void RePaint()
{
if (_backgroundImage == null)
{
_backgroundRect = ClientRectangle;
_backgroundImage = new Bitmap(_backgroundRect.Width, _backgroundRect.Height);
}
using (Graphics bufferedCanvas = Graphics.FromImage(_backgroundImage))
{
DrawAll(bufferedCanvas);
}
}
}
------解决方案--------------------你的绘图对象是从Image中生成的啊!怎么可能会显示在界面呢
用Paint事件中的Graphic对象
------解决方案--------------------RePaint 方法从当前控件创建画布
protected void RePaint()
{
using (Graphics bufferedCanvas = this.CreateGraphics()) {
DrawAll(bufferedCanvas);
}
}