如何在 WinRT 应用程序中进行快速图像处理?
How to do fast image processing in a WinRT application?
我正在开发一个应该需要一些图像处理的 WinRT 应用程序。
我使用 WriteableBitmapex
并尝试更改图像中椭圆上特定像素的颜色。
但我的解决方案非常非常慢。点击图像后大约几分钟。
为什么这个解决方案这么慢?以及替代方法是什么?
private void myimage_Tapped(object sender, TappedRoutedEventArgs e)
{
var xcor = e.GetPosition(sender as UIElement).X;
var ycor = e.GetPosition(sender as UIElement).Y;
Image ws = sender as Image;
int x = (int)(imag.PixelWidth * xcor / ws.ActualWidth);
int y = (int)(imag.PixelHeight * ycor / ws.ActualHeight);
var color = imag.GetPixel(x, y);
if (color.B != 0 && color.G != 0 && color.R != 0)
{
while (color.B != 0 && color.G != 0 && color.R != 0 && x < imag.PixelWidth)
{
x = x+1;
y = (int)(imag.PixelHeight * ycor / ws.ActualHeight);
while (color.B != 0 && color.G != 0 && color.R != 0 && y < imag.PixelHeight)
{
y = y + 1;
color = imag.GetPixel(x, y);
imag.SetPixel(x, y, Colors.Red);
}
}
}
}
使用BitmapContext
,因为Set/GetPixel
非常非常慢,实际上每次调用它们时它们open/close一个BitmapContext
检查你的逻辑,因为老实说它对我来说没有任何意义,或者解释你想要实现的目标
下面是一个关于如何在几个助手中使用上下文的例子:
var bitmap = BitmapFactory.New(2048, 2048);
using (var context = bitmap.GetBitmapContext(ReadWriteMode.ReadWrite))
{
Func<int, int, int> getPixel = (x, y) =>
{
var offset = y*bitmap.PixelWidth + x;
return offset;
};
Action<int, int, int> setPixel = (x, y, c) =>
{
var offset = getPixel(x, y);
context.Pixels[offset] = c;
};
Func<Color, int> toInt32 = c =>
{
var i = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
return i;
};
for (var y = 0; y < bitmap.PixelHeight; y++)
{
for (var x = 0; x < bitmap.PixelHeight; x++)
{
var color = Color.FromArgb(
255,
(byte) ((float) x/bitmap.PixelWidth*255),
(byte) ((float) y/bitmap.PixelHeight*255),
255);
var int32 = toInt32(color);
setPixel(x, y, int32);
}
}
}
我正在开发一个应该需要一些图像处理的 WinRT 应用程序。
我使用 WriteableBitmapex
并尝试更改图像中椭圆上特定像素的颜色。
但我的解决方案非常非常慢。点击图像后大约几分钟。
为什么这个解决方案这么慢?以及替代方法是什么?
private void myimage_Tapped(object sender, TappedRoutedEventArgs e)
{
var xcor = e.GetPosition(sender as UIElement).X;
var ycor = e.GetPosition(sender as UIElement).Y;
Image ws = sender as Image;
int x = (int)(imag.PixelWidth * xcor / ws.ActualWidth);
int y = (int)(imag.PixelHeight * ycor / ws.ActualHeight);
var color = imag.GetPixel(x, y);
if (color.B != 0 && color.G != 0 && color.R != 0)
{
while (color.B != 0 && color.G != 0 && color.R != 0 && x < imag.PixelWidth)
{
x = x+1;
y = (int)(imag.PixelHeight * ycor / ws.ActualHeight);
while (color.B != 0 && color.G != 0 && color.R != 0 && y < imag.PixelHeight)
{
y = y + 1;
color = imag.GetPixel(x, y);
imag.SetPixel(x, y, Colors.Red);
}
}
}
}
使用
BitmapContext
,因为Set/GetPixel
非常非常慢,实际上每次调用它们时它们open/close一个BitmapContext
检查你的逻辑,因为老实说它对我来说没有任何意义,或者解释你想要实现的目标
下面是一个关于如何在几个助手中使用上下文的例子:
var bitmap = BitmapFactory.New(2048, 2048);
using (var context = bitmap.GetBitmapContext(ReadWriteMode.ReadWrite))
{
Func<int, int, int> getPixel = (x, y) =>
{
var offset = y*bitmap.PixelWidth + x;
return offset;
};
Action<int, int, int> setPixel = (x, y, c) =>
{
var offset = getPixel(x, y);
context.Pixels[offset] = c;
};
Func<Color, int> toInt32 = c =>
{
var i = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
return i;
};
for (var y = 0; y < bitmap.PixelHeight; y++)
{
for (var x = 0; x < bitmap.PixelHeight; x++)
{
var color = Color.FromArgb(
255,
(byte) ((float) x/bitmap.PixelWidth*255),
(byte) ((float) y/bitmap.PixelHeight*255),
255);
var int32 = toInt32(color);
setPixel(x, y, int32);
}
}
}