把如何把BTYE数据弄成图片呈现到页面上
C# code
MemoryStream ms = new MemoryStream((byte[])sdr[0]);
System.Drawing.Image Img = System.Drawing.Image.FromStream(ms);
如何让页面的IMAGE控件 读取到这个图片?
------解决方案--------------------可以借鉴这种方法,不过不能用Image控件了.
在sqlserver中的图片类型是image
然后,通过dataset保存到数据库中,通过showimg.aspx文件来读出图片,即显示图片,代码如下:
Dim image As Byte() = IssueQuestionRow.QuestionImage
'/转换为支持存储区为内存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定义并实例化Bitmap对象
Dim bm As New Bitmap(memStream)
'/根据不同的条件进行输出或者下载;
Response.Clear()
'/如果请求字符串指定下载,就下载该文件;
'/否则,就显示在浏览器中。
If Request.QueryString("Download") = "1" Then
Response.Buffer = True
Response.ContentType = "application/octet-stream"
'/这里下载输出的文件名字 ok.jpg 为例子,你实际中可以根据情况动态决定。
Response.AddHeader("Content-Disposition", "attachment;filename=ok.jpg")
Else
Response.ContentType = "image/jpg"
End If
Response.BinaryWrite(image)
Response.End()
然后通过需要调用显示图片的页面,加入 <img src=”./showimg.aspx” wigth=”100px” height=”50”>
来固定图片的显示位置、大小等。
当然也可以通过一个页面的不同参数来获得不同的图片,如下代码:
Showimg.aspx文件:
Public QuestionID As String
Public ChapterID As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not IsPostBack Then
QuestionID = Request.QueryString("QID")
ChapterID = Request.QueryString("ChapterID")
Exercise = EXH.GetExercise(ChapterID)
Dim dv As New DataView(Exercise.Ex_IssueQuestion)
dv.RowFilter = "QuestionID='" + QuestionID + "'"
If dv.Count > 0 Then
IssueQuestionRow = dv.Item(0).Row
Dim image As Byte() = IssueQuestionRow.QuestionImage
'/转换为支持存储区为内存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定义并实例化Bitmap对象
Dim bm As New Bitmap(memStream)
'/根据不同的条件进行输出或者下载;
Response.BinaryWrite(image)
End If
End If
End Sub
在其他需要调用的地方的aspx页面里只需写:<img src=”./showimg.aspx?QuestionID=222&ChapterID=3” wigth=”100px” height=”50”>即可
------解决方案--------------------private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 12);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth-5, 20);
Graphics g = Graphics.FromImage(image);
//Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
Font f = new System.Drawing.Font("Times New Roman", 11, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.Magenta);
g.Clear(Color.LightBlue);
//g.DrawString(checkCode, f, b, 3, 3);
//g.DrawString(checkCode, f, b, 1, 2);
Pen p = new Pen(Color.SpringGreen, 0);
Random rand = new Random();
for (int i = 0; i < 3; i++)
{
g.DrawLine(p, rand.Next((int)(image.Width / 2)), rand.Next(image.Height), (int)(image.Width /