示例:
展示图像:http://www.scimence.club:8001/pages/pic.aspx?path=https://avatar.csdn.net/0/F/1/1_ljinshuan.jpg
path可修改为任意图像网址,或服务中系统路径如 (E:\tmp\QRTool\files\1.png)
http://localhost:12782/Application_new/Pages/pic.aspx?path=E:\tmp\QRTool\files\1.png
源码:
pic.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="pic.aspx.cs" Inherits="Pages_pic" %>
<%--<!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">
<div id ="tittleDiv" runat=server>
<title >
</title>
</div>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>--%>
pic.aspx.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.Net;
public partial class Pages_pic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string dirPath = Request.PhysicalApplicationPath + @"uploads\" + "1.png";
//Bitmap img = (Bitmap)Bitmap.FromFile(dirPath);
String dirPath = Request["path"]; // 本地或网络图像路径
String reName = Request["reName"]; // 重命名
bool isDownload = (Request["download"] != null && Request["download"].Equals("true")); // true时,直接弹出下载;false展示图像
//String alertMsg = Request["msg"];
if (dirPath == null)
{
Response.Write(Request.Url + "?" + "path=" + "http://t2.hddhhn.com/uploads/tu/201707/115/56.jpg" + "&" + "reName=" + "000.jpg" + "&" + "download=" + "false");
}
else
{
Bitmap img = getBitmap(dirPath);
if (reName == null) reName = System.IO.Path.GetFileName(dirPath);
// 输出图像
try
{
显示提示信息
//if (alertMsg != null && !alertMsg.Equals(""))
//{
// //Response.Write("<script>alert('" + alertMsg + "')</script>");
// tittleDiv.InnerHtml = "<title >"+alertMsg+"</title>";
//}
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "Image/png";
Response.AddHeader("Content-Disposition", (isDownload ? "attachment; " : "") + "filename=" + HttpUtility.UrlEncode(reName, System.Text.Encoding.UTF8));
Response.BinaryWrite(ms.ToArray());
}
catch (Exception ex) { }
}
}
/// <summary>
/// 获取本地获取网络路径中的图像
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private Bitmap getBitmap(String path)
{
Bitmap tmp = null;
try
{
if (path.ToLower().StartsWith("http://") || path.ToLower().StartsWith("https://"))
{
tmp = getWebImage(path);
}
else if (File.Exists(path))
{
tmp = (Bitmap)Bitmap.FromFile(path);
}
}
catch (Exception ex)
{
}
return tmp;
}
/// <summary>
/// 获取picUrl的图像
/// </summary>
private Bitmap getWebImage(String picUrl)
{
WebClient client = new WebClient();
byte[] data = client.DownloadData(picUrl); // 下载url对应图像数据
Bitmap image = null;
if (data.Length > 0) image = BytesToBitmap(data);// 转化为图像
return image;
}
/// <summary>
/// byte[] 转换 Bitmap
/// </summary>
public static Bitmap BytesToBitmap(byte[] Bytes)
{
MemoryStream stream = null;
try
{
stream = new MemoryStream(Bytes);
return new Bitmap(stream);
}
catch (Exception ex)
{ }
stream.Close();
return null;
}
}
动态引用示例:(多用于验证码动态显示逻辑,在.aspx中通过逻辑动态生成验证码图像)
<!DOCTYPE >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>图片横向居中</title>
</head>
<body>
<div align="center"><img src="http://60.205.185.168:8001/pages/pic.aspx?path=http://t2.hddhhn.com/uploads/tu/201707/115/56.jpg&reName=01.jpg" /></div>
</body>
</html>
注:当前文档中未展示验证码图像的生成,自定义逻辑生成验证码Bitmap图像就可以。