C# windows phone中的WriteableBitmapEx错误
嗨,我正在尝试在windows phone中使用,但代码不起作用…我在这方面做错了什么C# windows phone中的WriteableBitmapEx错误,c#,silverlight,windows-phone-7,windows-phone-7.1,C#,Silverlight,Windows Phone 7,Windows Phone 7.1,嗨,我正在尝试在windows phone中使用,但代码不起作用…我在这方面做错了什么 double height = image1.ActualHeight; double width = image1.ActualWidth; BitmapImage img = new BitmapImage(new Uri("Tulips.png", UriKind.RelativeOrAbsolute)); BitmapImage newI
double height = image1.ActualHeight;
double width = image1.ActualWidth;
BitmapImage img = new BitmapImage(new Uri("Tulips.png", UriKind.RelativeOrAbsolute));
BitmapImage newImg = image1.Source as BitmapImage;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int grayScale = (int)((image1.writeableBmp.GetPixel(j, i).R * 0.3) + (image1.writeableBmp.GetPixel(j, i).G * 0.59) + (image1.GetPixel(j, i).B * 0.11));
Color nc = Color.FromArgb (grayScale, grayScale, grayScale);
newImg.SetPixel(j, i, nc);
}
}
双倍高度=图像1.实际高度;
双倍宽度=图像1.实际宽度;
BitmapImage img=新的BitmapImage(新Uri(“Tulips.png”,UriKind.RelativeOrAbsolute));
BitmapImage newImg=image1。源为BitmapImage;
对于(int i=0;i
您正在尝试修改位图图像
(newImg
)。您必须创建一个WriteableBitmap
:
var newImg = new WriteableBitmap(image1.Source);
以便以后能够修改位图图像
然后(如果您正在引用WriteableBitmapEx),您应该能够直接从newImg
获取grayScale
表达式中的像素值:
byte grayScale = Convert.ToByte((newImg.GetPixel(j, i).R * 0.3) +
(newImg.GetPixel(j, i).G * 0.59) + (newImg.GetPixel(j, i).B * 0.11));
然后还有Color.FromArgb
语句,看起来应该更像这样:
Color nc = Color.FromArgb (255, grayScale, grayScale, grayScale);
你说“不工作”是什么意思?抛出异常时,什么也没有发生,结果不是您期望的结果?您遇到了什么样的错误?
grayScale[0]
只是一个输入错误吗?顺便说一句,Color.FromArgb()
有四个参数,表示Alpha
通道的第一个参数。@KooKiz errors int第二个for循环。.代码不能为空complied@AndersGustafsson是的,这是一个输入错误…第二个for循环仍然不工作,它在var newImg=new WriteableBitmap(image1.Source)中表示无效参数代码>和Color nc=Color.FromArgb(255,灰度,灰度,灰度)代码>它说不能从“int”转换为“byte”。没错,FromArgb
中的参数应该是byte
类型。使用byte grayScale=Convert.ToByte(…)
代替在grayScale
计算中进行转换。我将修改上面…关于构造中的错误,您是否有上面正确顺序的代码行?对于var newImg=new WriteableBitmap(image1.Source),可能应该从img
初始化newImg
代码>它表示无法从“System.Windows.Media.ImageSource”转换为“System.Windows.Media.Imaging.BitmapSource”