asp.net在MemoryStream中显示图像 [英] asp.net displaying image from MemoryStream

查看:266
本文介绍了asp.net在MemoryStream中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好每个人,



假设我有aspx



< asp:Image ID =Image1runat =server/>



和In .vb



进口System.Drawing.Text

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.IO

Imports System.Drawing.Imaging





公共类WebForm1

继承System.Web.UI.Page



受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.Load

Dim mem As MemoryStream = New MemoryStream

尝试

mem = fnx(rodolphe)

Catch ex As Exception

Response.Write( ex.Message)

最后

如果没有mem则什么都没有

mem.Dispose():mem = Nothing

结束如果



结束尝试



结束子



受保护函数fnx(ByVal str As String )作为MemoryStream

Dim ms作为MemoryStream = New MemoryStream()

尝试

Dim bitmap As New Bitmap(1,1)

Dim font As New Font(Arial,10,FontStyle.Regular,GraphicsUnit.Pixel)

昏暗的图形As Graphics = graphics.FromImage(bitmap)

Dim width As Integer = CInt(graphics.MeasureString(str,font).Width)

Dim height As Integer = CInt(graphics.MeasureString(str,font).Height)

bitmap =新位图(位图,新尺寸(宽度,高度))

graphics = graphics.FromImage(位图)

graphics.Clear(Color.White )

graphics.SmoothingMode = SmoothingMode.HighQuality

graphics.TextRenderingHint = TextRenderingHint.AntiAlias

graphics.DrawString(str,font,New SolidBrush(Color.FromArgb(5,5,5)),0,0)

graphics.Flush()

图形。 Dispose()

bitmap.Save(ms,ImageFormat.Jpeg)

返回ms

Catch ex As Exception

Response.Write(ex.Message)

结束尝试

结束功能

结束班级







如何将内存流转换为Image src

谢谢。 。 。

解决方案

hii我已经实现了以下这些内容请检查代码



这里我们有UploadeImage( )返回图像的函数字节



 MemoryStream ms = new MemoryStream(UploadImage()); 
img2.ImageUrl =data:image / png; base64,+ Convert.ToBase64String(ms.ToArray(),0,ms.ToArray()。Length);


我不知道vb.net中的语法,但是下面的C#代码几乎等于你想要实现的代码。您可以使用一些代码转换器来转换vb.net中的代码



使用(MemoryStream memoryStream = ...)

{

var imageSource = new BitmapImage();

imageSource.BeginInit();

imageSource.StreamSource = memoryStream;

imageSource.EndInit();



//分配图像的Source属性

image.Source = imageSource;

}


hello every body ,

assuming that I have in aspx

<asp:Image ID="Image1" runat="server" />

and In .vb

Imports System.Drawing.Text
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Drawing.Imaging


Public Class WebForm1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim mem As MemoryStream = New MemoryStream
Try
mem = fnx("rodolphe")
Catch ex As Exception
Response.Write(ex.Message)
Finally
If Not mem Is Nothing Then
mem.Dispose() : mem = Nothing
End If

End Try

End Sub

Protected Function fnx(ByVal str As String) As MemoryStream
Dim ms As MemoryStream = New MemoryStream()
Try
Dim bitmap As New Bitmap(1, 1)
Dim font As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel)
Dim graphics As Graphics = graphics.FromImage(bitmap)
Dim width As Integer = CInt(graphics.MeasureString(str, font).Width)
Dim height As Integer = CInt(graphics.MeasureString(str, font).Height)
bitmap = New Bitmap(bitmap, New Size(width, height))
graphics = graphics.FromImage(bitmap)
graphics.Clear(Color.White)
graphics.SmoothingMode = SmoothingMode.HighQuality
graphics.TextRenderingHint = TextRenderingHint.AntiAlias
graphics.DrawString(str, font, New SolidBrush(Color.FromArgb(5, 5, 5)), 0, 0)
graphics.Flush()
graphics.Dispose()
bitmap.Save(ms, ImageFormat.Jpeg)
Return ms
Catch ex As Exception
Response.Write(ex.Message)
End Try
End Function
End Class



how to can I transform the memorystream to Image src
thanks. . .

解决方案

hii i have implemented this things as below please check code

here we have UploadeImage() function which return image Bytes

MemoryStream ms = new MemoryStream(UploadImage());
img2.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray(), 0, ms.ToArray().Length);


I don't know about the syntax in vb.net, but following code of C# is almost equal to what you are trying to achieve. You can use some code converter to convert the code in vb.net

using (MemoryStream memoryStream = ...)
{
var imageSource = new BitmapImage();
imageSource.BeginInit();
imageSource.StreamSource = memoryStream;
imageSource.EndInit();

// Assign the Source property of your image
image.Source = imageSource;
}


这篇关于asp.net在MemoryStream中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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