C# 创建位图以按像素比较两个图像
我试着用像素来比较两幅图像。 我在谷歌上搜索过位图,但不清楚 我的代码显示错误C# 创建位图以按像素比较两个图像,c#,C#,我试着用像素来比较两幅图像。 我在谷歌上搜索过位图,但不清楚 我的代码显示错误参数无效 var img1 = new Bitmap(fileone); var img2 = new Bitmap(filetwo); 我试过这个 var a=新位图(imageurl) 但它不起作用 我也参考此网站进行图像比较: 我所尝试的: 但是在这一行参数上显示错误是无效的 var img1 = new Bitmap(fileone); var img2 = new Bitmap(filetwo); 我在
参数无效
var img1 = new Bitmap(fileone);
var img2 = new Bitmap(filetwo);
我试过这个
var a=新位图(imageurl)
但它不起作用
我也参考此网站进行图像比较:
我所尝试的:
但是在这一行参数上显示错误是无效的
var img1 = new Bitmap(fileone);
var img2 = new Bitmap(filetwo);
我在这两个变量中有存储路径,如下所示
fileone=C:\image\a.jpg:
filetwo=c:\image\b.jpg;
var img1 = new Bitmap(fileone);
var img2 = new Bitmap(filetwo);
if (img1.Width == img2.Width && img1.Height == img2.Height)
{
for (int i = 0; i < img1.Width; i++)
{
for (int j = 0; j < img1.Height; j++)
{
img1_ref = img1.GetPixel(i, j).ToString();
img2_ref = img2.GetPixel(i, j).ToString();
if (img1_ref != img2_ref)
{
count2++;
flag = false;
break;
}
count1++;
}
// progressBar1.Value++;
}
if (flag == false)
Response.Write("Sorry, Images are not same , " + count2 + " wrong pixels found");
else
Response.Write(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found");
}
else
Response.Write("can not compare this images");
this.Dispose();
fileone=C:\image\a.jpg:
filetwo=c:\image\b.jpg;
var img1=新位图(fileone);
var img2=新位图(filetwo);
if(img1.Width==img2.Width&&img1.Height==img2.Height)
{
对于(int i=0;i
如果您在
var img1 = new Bitmap(fileone);
很可能您的fileone不存在,请确保C:\image\a.jpg
存在
那是无效的。如何引用文件名非常重要,因为反斜杠是一个转义字符
也许你真正的代码看起来像
fileone="C:\image\a.jpg";
filetwo="C:\image\b.jpg";
然后,\b
会变成一个退格字符,而不是您想要的字符。可以使用@-literal来避免转义处理:
fileone=@"C:\image\a.jpg";
filetwo=@"C:\image\b.jpg";
编译或运行程序时是否出现错误?代码看起来不错,所以我猜是在运行时,fileone/filetwo的值不正确(可能没有正确转义?)。我检查了fileone和filetwo存储的正确路径以及该文件夹中的图像。如果您看到错误,请提供异常详细信息[ArgumentException:参数无效。]System.Drawing.Bitmap..ctor(字符串文件名)+375973 validate.Day_8_campairTwoImageUpload.Button1_单击C:\Users\saisoftex\Documents\Visual Studio 2008\Projects\validate\validate\Day_8_campairTwoImageUpload.aspx.cs:120 System.Web.UI.WebControls.Button.OnClick(EventArgs e)+111 System.Web.UI.WebControl.Button.RaisePostBackEvent(String eventArgument)+110您的路径“C:\image\a.jpg”可能需要转义“C:\\image\\a.jpg”和额外的\i选中的图像在该文件夹中可用。