将jpg转换为png并分配给图像控件

Convert jpg to png and assign to image control
2021-05-11
  •  译文(汉语)
  •  原文(英语)

我正在页面之间传递图像.但是在第二页上,我想将jpg图像转换为具有透明背景的png图像.最后,我想将转换后的图像分配给Image控件,但是隐式转换类型出错.

这是我的代码:

第一页:

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'

解决过程1

您可以使用WriteableBitmapEx库来创建WriteableBitmap.

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

icon.Source = wb;

您可以从nuget获取WriteableBitmapEx库.

速聊1:
现在图像模糊完全发送错误.像是同一行,但颜色不同.
速聊2:
我将使用WriteableBitmapEx中提供的ToByteArray扩展方法,然后使用FromByteArray方法.
解决过程2

ExtendedImage如果要将其分配给Image控件,则需要转换:

icon.Source = myImage.ToBitmap();
速聊1:
好吧,您发布的代码没有多大意义.您正在检索位图的二进制数据,将其放入WriteableBitmap中,然后将其保存为png.那时,您可以直接将WriteableBitmap分配给Image控件.
速聊2:
好的,但是如何将位图的二进制数据放入具有透明背景的WriteableBitmap中呢?
速聊3:
在WriteableBitmap中,像素存储在ARGB中.即:alpha,红色,绿色,蓝色.通过更改Alpha通道的值,可以更改像素的透明度

I'm passing images between pages. But on the second page I want to convert image which is jpg to png with transparent background. Finally I want to assign converted image to Image control, but I have error with implicitly convert type.

Here is my code:

First page:

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

Second page:

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();

I'm using ImageTools library.

Error message:

Error 1 Cannot implicitly convert type 'ImageTools.ExtendedImage' to 'System.Windows.Media.ImageSource'

Solutions1

You could use the WriteableBitmapEx library to create a WriteableBitmap.

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

icon.Source = wb;

You can get the WriteableBitmapEx library from nuget.

Talk1:
Now the image is fuzzy completely wrong sent. Something like the same lines in different colors.
Talk2:
I would use the ToByteArray extension method provided in WriteableBitmapEx and then the FromByteArray method.
Solutions2

You need to convert your ExtendedImage if you want to assign it to your Image control:

icon.Source = myImage.ToBitmap();
Talk1:
Well, the code you've posted doesn't make much sense. You're retrieving the bitmap's binary data, putting it in a WriteableBitmap, then saving it in a png. At that point, you could assign directly the WriteableBitmap to the Image control
Talk2:
Okay, but how can I put bitmap's binary data in a WriteableBitmap with transparent background ?
Talk3:
In the WriteableBitmap, pixels are stored in ARGB. That is: alpha, red, green, blue. By altering the value of the alpha channel, you can change the transparency of the pixel
转载于:https://stackoverflow.com/questions/20199984/convert-jpg-to-png-and-assign-to-image-control

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入