Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 覆盖Picturebox OnPaint事件以旋转图像-创建自定义Picturebox_C#_.net_Picturebox_System.drawing - Fatal编程技术网

C# 覆盖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)); } 如何访问图像以及如何创建自定义控件 下面是子类的一个快

我想创建一个自定义控件或覆盖PictureBox的onpaint事件,以便在PictureBox中绘制图像之前访问图像,以便旋转图像

我知道我可以做这样的事情

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中分配图像时崩溃过,但无法复制

我不认为图像是用onPaint方法绘制的;也不是背景图像。。这实际上是三个不同的东西,或者说是显示的层次。但是我想你可以隐藏图像属性并用你自己的替换它..我能够将有问题的位图bmp=值复制为位图;在最初运行代码时引发空引用异常。我最初没有设置此属性。可以使用
DesignMode
属性排除某些行,当然也可以检查
值!=空