我试图从位图转换为base64,从base64转换为Image,因为我想在名为Image1的图像字段上显示屏幕截图。问题在于,我无法将值imagen分配给Image,它表示"类型的值' System.Drawing.Image'无法转换为' System.Web.UI.WebControls.Image'
Imports System.Drawing
Imports System.Windows.Forms
Public Class fotoPrueba
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim bmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim base As String
Dim imagen As Image
base = BitmapToBase64(bmp)
imagen = Base64ToImage(base)
Image1 = imagen
End Sub
Public Function BitmapToBase64(ByVal image As System.Drawing.Bitmap) As String
Dim base64 As String
Dim memory As New System.IO.MemoryStream()
image.Save(memory, Imaging.ImageFormat.Bmp)
base64 = System.Convert.ToBase64String(memory.ToArray)
memory.Close()
memory = Nothing
Return base64
End Function
Function Base64ToImage(ByVal base64string As String) As System.Drawing.Image
'Setup image and get data stream together
Dim img As System.Drawing.Image
Dim MS As System.IO.MemoryStream = New System.IO.MemoryStream
Dim b64 As String = base64string.Replace(" ", "+")
Dim b() As Byte
'Converts the base64 encoded msg to image data
b = Convert.FromBase64String(b64)
MS = New System.IO.MemoryStream(b)
'creates image
img = System.Drawing.Image.FromStream(MS)
Return img
End Function
End Class