覆盖 Picturbox OnPaint 事件以旋转图像 - 创建自定义 Picturebox
Override Picturbox OnPaint Event to Rotate the Image -Create Custom Picturebox
我想创建一个自定义控件或重写 pictuebox 的 onpaint 事件,这样我就可以在将图像绘制到 picturbox 之前访问图像,这样我就可以旋转图像。
我知道我可以做这样的事情
private void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(10, 10, 20, 20));
}
如何访问图像以及如何创建自定义控件。
这是一个子类的简单示例:它隐藏了原始的 Image
属性 并用在分配之前进行旋转的子类替换它:
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
分配的图像。
- 我在设计器中分配图像时遇到过一次崩溃,但无法重现。
我想创建一个自定义控件或重写 pictuebox 的 onpaint 事件,这样我就可以在将图像绘制到 picturbox 之前访问图像,这样我就可以旋转图像。
我知道我可以做这样的事情
private void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawRectangle(Pens.Black, new Rectangle(10, 10, 20, 20));
}
如何访问图像以及如何创建自定义控件。
这是一个子类的简单示例:它隐藏了原始的 Image
属性 并用在分配之前进行旋转的子类替换它:
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
分配的图像。 - 我在设计器中分配图像时遇到过一次崩溃,但无法重现。