.net 将图片文件转换成流输出到浏览器

时间:2022-10-21 14:31:56

aspx 页面

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <img alt="" src="data:image/jpeg;base64,<%=Img%>" />
    </div>
    </form>
</body>
</html>

 

 

CS页面

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

public partial class Default6 : System.Web.UI.Page
{
    protected string Img { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string path = @"C:\Users\Administrator\Desktop\0314股票医生开机画面\p1.png";
            if (!string.IsNullOrEmpty(path))
            {
                System.Drawing.Image _Image = System.Drawing.Image.FromFile(path);
                System.IO.MemoryStream _ImageMem = new System.IO.MemoryStream();
                _Image.Save(_ImageMem, ImageFormat.Jpeg);
                byte[] _ImageBytes = _ImageMem.ToArray();
                Img = Convert.ToBase64String(_ImageBytes);
                //Response.Write(Img);
            }
        }
    }
}