C# 覆盖Picturebox OnPaint事件以旋转图像-创建自定义Picturebox
我想创建一个自定义控件或覆盖PictureBox的onpaint事件,以便在PictureBox中绘制图像之前访问图像,以便旋转图像 我知道我可以做这样的事情C# 覆盖Picturebox OnPaint事件以旋转图像-创建自定义Picturebox,c#,.net,picturebox,system.drawing,C#,.net,Picturebox,System.drawing,我想创建一个自定义控件或覆盖PictureBox的onpaint事件,以便在PictureBox中绘制图像之前访问图像,以便旋转图像 我知道我可以做这样的事情 private void pictureBox1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(Pens.Black, new Rectangle(10, 10, 20, 20)); } 如何访问图像以及如何创建自定义控件 下面是子类的一个快
private void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(10, 10, 20, 20));
}
如何访问图像以及如何创建自定义控件 下面是子类的一个快速示例:它隐藏原始的
图像
属性,并将其替换为在指定之前进行旋转的属性:
class RotatedPictureBox : PictureBox
{
private Image image;
public new Image Image {
get { return image; } // ?? you may want to undo the rotation here ??
set {
Bitmap bmp = value as Bitmap ;
// use the rotation you need!
if ( bmp != null ) bmp.RotateFlip(RotateFlipType.Rotate270FlipX);
image = bmp;
base.Image = Image;
}
}
}
public RotatedPictureBox ()
{
}
}
警告:分配图像似乎可行,但我没有测试它的所有可能用途。。已知的限制
- 它不会旋转通过
指定的图像ImageLocation
- 我曾经在Designer中分配图像时崩溃过,但无法复制
DesignMode
属性排除某些行,当然也可以检查值!=空
。