如何使用Graphics类在PictureBox控件内绘制矩形? [英] How do i draw rectangle inside picturebox control using Graphics class?
本文介绍了如何使用Graphics类在PictureBox控件内绘制矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在知道图片框控件内的欧姆值后绘制电阻器图像.我无法访问图片框内的creategraphics.我怎么做?请帮忙.
i want to draw a resistor image after knowing a value in ohm inside a picturebox control. i cannnot access creategraphics inside picture box. how do i do that? please help.
推荐答案
您应该使用PictureBox.Image
的Graphics
,而不是Picturebox
本身.像这样:
You should use theGraphics
of thePictureBox.Image
, not of thePicturebox
itself. Like this:
Graphics g = Graphics.FromImage(pictureBox1.Image);
您可以简单地将PictureBox
类子类化并使用它.
对OnPaint
方法的重写应允许您绘制所需的任何内容.
这样的事情应该可以解决问题:
You could simply subclass thePictureBox
class and use that.
An override on theOnPaint
method should allow you to draw whatever you want.
Something like this should do the trick:
class CustomPictureBox : PictureBox
{
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.DrawRectangle(Pens.Aquamarine, new Rectangle(0, 0, 4, 4));
}
}
希望这会有所帮助,
弗雷德里克·博纳德(Fredrik Bornander)
Hope this helps,
Fredrik Bornander
这篇关于如何使用Graphics类在PictureBox控件内绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文