[英]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
非常慢 ,實際上每次調用它們都會打開/關閉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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.