将Base64 png图像更改为jpeg [英] Change Base64 png image to jpeg

查看:85
本文介绍了将Base64 png图像更改为jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将base64 png编码的字符串转换为jpeg图像,然后将jpeg保存到sql 2008图像字段?下面的代码用于显示我希望实现的内容,但会在Imageformat上出现对话错误。



How would I convert a base64 png encoded string to a jpeg image and then save the jpeg to sql 2008 image field? The code below is used to show what I hope to achieve but breaks with a conversation error on the Imageformat.

The base64 string: data:image/png;base64,iVBORw0KGgoAA etc......










Public Function SaveBase64ToImage(ByVal base64 As String)
        Try
            Using ms As New MemoryStream(Convert.FromBase64String(base64))
                Using bm As New Bitmap(ms)
                    bm.Save(ms, System.Drawing.Imaging.ImageFormat.jpg)
                    Return bm
                End Using
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function





我添加了一个单独的功能out内存流,但当我尝试使用该函数为sql图像提供数据保存然后从表中检索图像时,我得到一个黑色方块而不是原始图像!





I have added a function to separate out the memory stream, but when I attempt to use the function to provide the data for the sql image save and then retrieve the image from the table and i get a black square and not the original image!

Public Shared Function ImageToBytes(img As Image) As Byte()
        If img Is Nothing Then Return Nothing
        Dim ms As New MemoryStream()
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)  
        Return ms.ToArray()
    End Function

推荐答案

来自 文档 [ ^ ]:

From "The Documentation"[^]:
您应该避免将图像保存到用于构建它的相同流中。这样做可能会损坏流。
You should avoid saving an image to the same stream that was used to construct it. Doing so might damage the stream.


这篇关于将Base64 png图像更改为jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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