2013-11-25 95 views
0

我在页面之间传递图像。但在第二页上,我想将图像转换为带有透明背景的png。 最后我想分配转换后的图像到图像控件,但我有隐式转换类型的错误。将jpg转换为png并分配给图像控件

这里是我的代码:

第一页:

WriteableBitmap wb = new WriteableBitmap(logoQrCodeImage, null); 
Byte[] array = ConvertImage.ConvertToBytes(wb); 
if (!IsolatedStorageSettings.ApplicationSettings.Contains("State")) 
{ 
    IsolatedStorageSettings.ApplicationSettings["State"] = array; 
    IsolatedStorageSettings.ApplicationSettings.Save(); 
} 

第二页:

Byte[] array = IsolatedStorageSettings.ApplicationSettings["State"] as Byte[]; 
MemoryStream stream = new MemoryStream(array); 
WriteableBitmap wb = new WriteableBitmap(50, 50); 
//wb.LoadJpeg(stream); 

var encoder = new PngEncoder(); 
MemoryStream pngStream = new MemoryStream(); 
ExtendedImage myImage; 
myImage = wb.ToImage(); 
encoder.Encode(myImage, stream); 

icon.Source = myImage; //ERROR 

IsolatedStorageSettings.ApplicationSettings.Remove("State"); 
IsolatedStorageSettings.ApplicationSettings.Save(); 

我使用ImageTools库。

错误消息:

错误1无法隐式转换类型 'ImageTools.ExtendedImage' 到 'System.Windows.Media.ImageSource'

回答

0

您可以使用WriteableBitmapEx库创建一个WriteableBitmap的。

WriteableBitmap wb = new WriteableBitmap(50, 50); 
wb.FromByteArray(array); 

icon.Source = wb; 

您可以从nuget获得WriteableBitmapEx库。

+0

现在图像模糊完全错误发送。像不同颜色的相同线条。 – user2962457

+0

我有这个:[Image](http://i.imgur.com/9iiIWBJ.jpg) – user2962457

+0

我会使用WriteableBitmapEx中提供的ToByteArray扩展方法,然后使用FromByteArray方法。 –

1

您需要将ExtendedImage转换,如果你想其分配到Image控件:

icon.Source = myImage.ToBitmap(); 
+0

之后,我没有错误,但图像不显示。 – user2962457

+0

@ user2962457那么,你发布的代码没有多大意义。您正在检索位图的二进制数据,将其放入WriteableBitmap中,然后将其保存为png。此时,您可以直接将WriteableBitmap分配给Image控件 –

+0

好吧,但是如何将位图的二进制数据放入具有透明背景的WriteableBitmap? – user2962457