Windows 8 如何在win8应用程序中获取图像像素
在c#中,我们可以使用像素来比较图像Windows 8 如何在win8应用程序中获取图像像素,windows-8,windows-runtime,Windows 8,Windows Runtime,在c#中,我们可以使用像素来比较图像 Bitmap b1 = new Bitmap(textBox1.Text); Bitmap b2 = new Bitmap(textBox2.Text); for (int x = 0; x < b1.Width; x++) { for (int y = 0; y < b1.Height; y++)
Bitmap b1 = new Bitmap(textBox1.Text);
Bitmap b2 = new Bitmap(textBox2.Text);
for (int x = 0; x < b1.Width; x++)
{
for (int y = 0; y < b1.Height; y++)
{
if (b1.GetPixel(x, y) != b2.GetPixel(x, y))
{
Console.Out.Write("Mismatch");
}
}
}
Bitmap b1=新位图(textBox1.Text);
位图b2=新位图(textBox2.Text);
对于(int x=0;x
在winRT应用程序中,像素如何通过这种方式进行比较
提前感谢使用系统;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
namespace App110
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Test();
}
private async void Test()
{
// Comparing image to itself should return true
Debug.Assert(await ComparePackageImages("Assets\\Logo.png", "Assets\\Logo.png"));
// "Copy of Logo.png" is "Logo.png" with changed one pixel
// and should return false when compared with "Logo.png"
Debug.Assert(!await ComparePackageImages("Assets\\Logo.png", "Assets\\Copy of Logo.png"));
// Two images of different size should
Debug.Assert(!await ComparePackageImages("Assets\\Logo.png", "Assets\\SmallLogo.png"));
await new MessageDialog("Success!").ShowAsync();
Application.Current.Exit();
}
private async Task<bool> ComparePackageImages(string packageFilePath1, string packageFilePath2)
{
var b1 = await LoadWriteableBitmapFromPackageFilePath(packageFilePath1);
var b2 = await LoadWriteableBitmapFromPackageFilePath(packageFilePath2);
var pixelStream1 = b1.PixelBuffer.AsStream();
var pixelStream2 = b2.PixelBuffer.AsStream();
return StreamEquals(pixelStream1, pixelStream2);
}
private static async Task<WriteableBitmap> LoadWriteableBitmapFromPackageFilePath(string packageFilePath1)
{
var bitmap = new WriteableBitmap(1, 1);
var storageFile = await Package.Current.InstalledLocation.GetFileAsync(packageFilePath1);
using (var streamWithContentType = await storageFile.OpenReadAsync())
{
await bitmap.SetSourceAsync(streamWithContentType);
}
return bitmap;
}
static bool StreamEquals(Stream stream1, Stream stream2)
{
const int bufferSize = 2048;
byte[] buffer1 = new byte[bufferSize]; //buffer size
byte[] buffer2 = new byte[bufferSize];
while (true)
{
int count1 = stream1.Read(buffer1, 0, bufferSize);
int count2 = stream2.Read(buffer2, 0, bufferSize);
if (count1 != count2)
return false;
if (count1 == 0)
return true;
// You might replace the following with an efficient "memcmp"
if (!buffer1.Take(count1).SequenceEqual(buffer2.Take(count2)))
return false;
}
}
}
}
使用系统诊断;
使用System.IO;
使用System.Linq;
使用System.Runtime.InteropServices.WindowsRuntime;
使用System.Threading.Tasks;
使用Windows.ApplicationModel;
使用Windows.UI.Popups;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Media.Imaging;
名称空间App110
{
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
Test();
}
专用异步无效测试()
{
//将图像与自身进行比较应返回true
Assert(等待ComparePackageImages(“Assets\\Logo.png”、“Assets\\Logo.png”);
//“Logo.png的副本”是“Logo.png”,更改了一个像素
//与“Logo.png”相比,应返回false
Assert(!wait wait ComparePackageImages(“Assets\\Logo.png”,“Assets\\Copy of Logo.png”);
//两个不同大小的图像应该
Assert(!wait wait ComparePackageImages(“Assets\\Logo.png”、“Assets\\SmallLogo.png”);
等待新消息对话框(“成功!”).ShowAsync();
Application.Current.Exit();
}
专用异步任务ComparePackageImages(字符串packageFilePath1、字符串packageFilePath2)
{
var b1=等待LoadWriteableBitmapFromPackageFilePath(packageFilePath1);
var b2=等待LoadWriteableBitmapFromPackageFilePath(packageFilePath2);
var pixelStream1=b1.PixelBuffer.AsStream();
var pixelStream2=b2.PixelBuffer.AsStream();
返回流质量(像素流1、像素流2);
}
私有静态异步任务LoadWriteableBitmapFromPackageFilePath(字符串packageFilePath1)
{
var位图=新的可写位图(1,1);
var storageFile=await Package.Current.InstalledLocation.GetFileAsync(packageFilePath1);
使用(var streamWithContentType=await-storageFile.OpenReadAsync())
{
wait bitmap.SetSourceAsync(streamWithContentType);
}
返回位图;
}
静态布尔流质量(流1、流2)
{
const int bufferSize=2048;
byte[]buffer1=新字节[bufferSize];//缓冲区大小
byte[]buffer2=新字节[bufferSize];
while(true)
{
int count1=stream1.Read(buffer1,0,bufferSize);
int count2=stream2.Read(buffer2,0,bufferSize);
如果(count1!=count2)
返回false;
如果(count1==0)
返回true;
//您可以将以下内容替换为有效的“memcmp”
if(!buffer1.Take(count1).SequenceEqual(buffer2.Take(count2)))
返回false;
}
}
}
}
提供的流比较
位图访问代码片段,由提供