用于图像的嵌入式base64String对于大图像来说是长的吗?

时间:2016-11-14 15:13:43

标签: vb.net tinymce base64

我将一个base64String嵌入到Tinymce中以显示图像:

<img src="blob:https://path/871bf236-3bae-472c-9f02-0bd3be19a435" alt="Desert.jpg" width="300" height="75" />

它适用于小图像但是当它涉及大图像时它不起作用而我猜测它因为包含大图像的base64String的URL达到了URL浏览器长度的限制,我相信它大约是2000个字符。想看看是否有办法缩短我的base64String?

'File path of the attachment 
DIM filePath = C:\path\solutions\Attachments\1\1726014c-7a2d-41b8-a79e-2acec1e8c7e0

'Converted base64String path 
DIM base64URLPath = ToBase64String( ConvertToUrl(filePath)).toString

'Converts the path to a base64String
Public Function ToBase64String(filePath As String) As String
        Dim aImage = New Bitmap(filePath)
        Using stream = New IO.MemoryStream
            Using img As Image = Image.FromFile(filePath)
                If img.RawFormat.Equals(Imaging.ImageFormat.Jpeg) Then
                    aImage.Save(stream, Imaging.ImageFormat.Jpeg)
                ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Png) Then
                    aImage.Save(stream, Imaging.ImageFormat.Png)
                ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Icon) Then
                    aImage.Save(stream, Imaging.ImageFormat.Icon)
                End If
            End Using
            Return Convert.ToBase64String(stream.ToArray)
        End Using
    End Function

  'Gets the full file path 
  Public Function ConvertToUrl(filePath As String) As String
        Dim uri = New Uri(filePath).LocalPath
        Dim converted = uri
        Return converted.ToString()
    End Function

1 个答案:

答案 0 :(得分:0)

根据这篇文章我修正了问题:Resize and Compress image to byte array without saving the new image。 ToBase64String是唯一改变的功能,现在它看起来像:

'Converts the path to a base64String
 Public Function ToBase64String(filePath As String) As String
        Dim aImage = New Bitmap(filePath)
        Dim aspectRatio As Double = aImage.Height / aImage.Width
        Dim imgThumb = New Bitmap(aImage, 200, CInt(Math.Round(200 * aspectRatio)))
        Using stream = New IO.MemoryStream
            Using img As Image = Image.FromFile(filePath)
                If img.RawFormat.Equals(Imaging.ImageFormat.Jpeg) Then
                  imgThumb.Save(stream, Imaging.ImageFormat.Jpeg)
                ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Png) Then
                   imgThumb.Save(stream, Imaging.ImageFormat.Png)
                ElseIf img.RawFormat.Equals(Imaging.ImageFormat.Icon) Then
                   imgThumb.Save(stream, Imaging.ImageFormat.Icon)
                End If
            End Using
            Return Convert.ToBase64String(stream.ToArray)
        End Using
    End Function