GetPixel()函数出错
本文关键字:出错 函数 GetPixel | 更新日期: 2023-09-27 18:18:28
getpixel函数.....出现错误虽然使用了图像比较所需的所有名称空间.....我在visual studio 2013中制作Windows应用....我是否需要在任何地方定义getpixel()函数…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using picture.Resources;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using Microsoft.Expression.Drawing;
namespace picture {
public partial class MainPage: PhoneApplicationPage {
// Constructor
public MainPage() {
InitializeComponent();
BitmapImage image1 = new BitmapImage();
image1.SetSource(Application.GetResourceStream(new Uri(@
"Assets/WP_20150925_09_20_03_Pro (2).jpg", UriKind.Relative)).Stream);
BitmapImage image2 = new BitmapImage();
image2.SetSource(Application.GetResourceStream(new Uri(@
"Assets/WP_20150925_09_20_03_Pro.jpg", UriKind.Relative)).Stream);
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private double get_Percentage_Method(WriteableBitmap wb1, WriteableBitmap wb2) {
var img1 = wb1;
var img2 = wb2;
// Validation of image size code
int width1 = img1.PixelWidth;
int width2 = img2.PixelWidth;
int height1 = img1.PixelHeight;
int height2 = img2.PixelHeight;
if ((width1 != width2) || (height1 != height2)) {
MessageBox.Show("Error: Images dimensions mismatch");
}
long diff = 0;
for (int i = 0; i < width1; i++) {
for (int j = 0; j < height1; j++) {
// get same position pixel of both WirtableBitmap Image object
Color color1 = wb1.GetPixel(i, j);
Color color2 = wb2.GetPixel(i, j);
// converting each value iGetPixelnto Integer
int r1 = (int) color1.R;
int g1 = (int) color1.G;
int b1 = (int) color1.B;
int r2 = (int) color2.R;
int g2 = (int) color1.G;
int b2 = (int) color1.B;
// calculating difference between RGB color value and adding it in Diff variable.
diff += Math.Abs(r1 - r2);
diff += Math.Abs(g1 - g2);
diff += Math.Abs(b1 - b2);
}
}
// Converting difference into percentage and then retun it.
double n = width1 * height1; //* 3;
double p = diff / n / 255.0;
MessageBox.Show("diff percent: " + (p * 100.0));
return (p * 100.0);
}
}
}
你会在那一行得到一个错误
Color color1 = wb1.GetPixel(i, j);
这是因为wb1
在
WriteableBitmap
类型 private double get_Percentage_Method(WriteableBitmap wb1, WriteableBitmap wb2)
,这反过来是因为WriteableBitmap
类没有GetPixel()
方法,正如您可以在MSDN文档中看到的那样。要么使用另一个类作为输入(例如,一个普通的Image
或Bitmap
将已经做),或者通过使用CopyPixels()
方法(MSDN)来解决它,在这个方法中,你将不得不传递一个你想要的像素坐标数组,以及其他图像相关参数。
您的wb1/wb2类型为WriteableBitmap,但WriteableBitmap没有GetPixel()
功能。我假设你正在考虑Bitmap.GetPixel().
我建议问另一个问题关于如何获得WriteableBitmap的像素