将BitmapImage透明地转换为base64字符串并将其解码回来 [英] Convert BitmapImage with transparency to base64 string and decode it back

查看:115
本文介绍了将BitmapImage透明地转换为base64字符串并将其解码回来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试将Image控件(类型为BitmapImage)的源保存到字符串中,然后将其解码回来。


我已经使用以下函数来执行这些操作:

公共函数ConvertImageToString(Image as BitmapImage)As String 
Dim byteArray As Byte()
使用stream As New MemoryStream()
Dim bmp As New WriteableBitmap(DirectCast(Image,BitmapImage))
bmp.SaveJpeg(stream,bmp.PixelWidth,bmp .PixelHeight,0,100)
byteArray = stream.ToArray()
结束使用
返回Convert.ToBase64String(byteArray)
结束函数

公开函数ConvertStringToImage(Text As String)As BitmapImage
Dim filebytes()= Convert.FromBase64String(Text)
使用ms作为新的MemoryStream(filebytes,0,filebytes.Length)
ms.Write( filebytes,0,filebytes.Length)
昏暗图像As New BitmapImage
image.SetSource(ms)
返回图片
结束使用
结束函数

此代码仅适用于没有透明度的图像。如果我尝试解码表示具有透明度的位图的字符串,则图像被成功加载但是"透明部分"被加载。充满了黑色。


有关如何解决这个问题的想法吗?


提前致谢。




Gianluca Concilio

解决方案

JPG格式不支持透明度,所以当ConvertImageToString调用SaveJpeg时透明部分是黑色的。


如果您的透明部分都是完全透明的,那么您可以在ConvertImageToString中处理WriteableBitmap以使用键颜色替换alpha 0像素(传统上是洋红色,但您可以使用任何不会出现在
图像中的东西)然后让ConvertStringToImage处理像素缓冲区,将关键颜色转换回alpha 0像素。


- Rob


Hi all,

I'm trying to save the source of an Image control (of type BitmapImage) to a string and then decode it back.

I already use the following functions to do these operations:

    Public Function ConvertImageToString(Image As BitmapImage) As String
        Dim byteArray As Byte()
        Using stream As New MemoryStream()
            Dim bmp As New WriteableBitmap(DirectCast(Image, BitmapImage))
            bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100)
            byteArray = stream.ToArray()
        End Using
        Return Convert.ToBase64String(byteArray)
    End Function

    Public Function ConvertStringToImage(Text As String) As BitmapImage
        Dim filebytes() = Convert.FromBase64String(Text)
        Using ms As New MemoryStream(filebytes, 0, filebytes.Length)
            ms.Write(filebytes, 0, filebytes.Length)
            Dim image As New BitmapImage
            image.SetSource(ms)
            Return image
        End Using
    End Function

this code works fine only with images with no transparency. if i try to decode the string representing a bitmapimage with transparency, the image is succesfully loaded but the "transparent parts" are filled with black.

Any idea on how to solve this problem?

Thanks in advance.


Gianluca Concilio

解决方案

The JPG format doesn't support transparency, so when ConvertImageToString calls SaveJpeg the transparent parts are left black.

If your transparent sections are all fully transparent then you can process the WriteableBitmap in ConvertImageToString to replace the alpha 0 pixels with a key colour (traditionally magenta, but you can use anything that doesn't otherwise appear in your image) and then have ConvertStringToImage process the pixel buffer to convert the key colour back to alpha 0 pixels.

--Rob


这篇关于将BitmapImage透明地转换为base64字符串并将其解码回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆