C# 较大的图像旋转会导致红色X

C# 较大的图像旋转会导致红色X,c#,aforge,C#,Aforge,我正在尝试使用一个旋转图像。以下是代码(在图片框的绘制事件方法中): 如果我取出RotateBilear线和ro.Apply线,它会很好地绘制原始图像,但是当我尝试旋转它时,表单的背景会变为白色,并且表单周围会出现一个红色的x,轮廓为红色。有人知道这个问题吗?当代码生成异常错误时,您会得到红色的X 我看不出您在哪里分配了agent或argent.image,但您使用了这些变量/属性。我怀疑你有一些空值或类似值,你真的想写: image = ro.Apply(image); 因为您最初设置了im

我正在尝试使用一个旋转图像。以下是代码(在图片框的绘制事件方法中):


如果我取出RotateBilear线和ro.Apply线,它会很好地绘制原始图像,但是当我尝试旋转它时,表单的背景会变为白色,并且表单周围会出现一个红色的x,轮廓为红色。有人知道这个问题吗?

当代码生成异常错误时,您会得到红色的X

我看不出您在哪里分配了
agent
argent.image
,但您使用了这些变量/属性。我怀疑你有一些空值或类似值,你真的想写:

image = ro.Apply(image);

因为您最初设置了
image
,并在
DrawImage
调用中使用它。

尝试在代码周围添加Try/catch

int radius = 10; 
Image img = new Bitmap("file_path.png");
Bitmap image = new Bitmap(img, radius, radius);
//arbitrary angle of 67

try
{
    image = AForge.Imaging.Image.Clone(image, PixelFormat.Format8bppIndexed);
    RotateBilinear ro = new RotateBilinear(67, true);
    image = ro.Apply(image);
}
catch (Exception e)
{ 
    Debug.WriteLine(string.Format("Caught Exception {0}\n{1}", e.GetType(), e.Message);
} 

Graphics g = e.Graphics;
g.DrawImage(image, 100, 100);

如果我的怀疑是正确的,您将使红色x消失,图像将被渲染为未旋转。在VisualStudio的输出窗口中会有一些跟踪语句,告诉您异常类型和异常消息。这些信息应该可以让您了解出了什么问题。

我在这里写错了,但我已经修复了它,我确实使用了我在这种方法中设置的图像,但它仍然不起作用。我收到了错误消息->“过滤器不支持源像素格式”。我不确定我需要对png做什么更改才能使其正常工作。快速搜索一下,建议您可能必须使用适当的像素格式选项创建图像。我已经在try子句中添加了相应的代码-从未使用过AForge库,我无法保证使用的是确切的PixelFormat枚举器,但是您应该能够通过查看文档找到可以使用的。
int radius = 10; 
Image img = new Bitmap("file_path.png");
Bitmap image = new Bitmap(img, radius, radius);
//arbitrary angle of 67

try
{
    image = AForge.Imaging.Image.Clone(image, PixelFormat.Format8bppIndexed);
    RotateBilinear ro = new RotateBilinear(67, true);
    image = ro.Apply(image);
}
catch (Exception e)
{ 
    Debug.WriteLine(string.Format("Caught Exception {0}\n{1}", e.GetType(), e.Message);
} 

Graphics g = e.Graphics;
g.DrawImage(image, 100, 100);