将图片资源文件整合到DLL文件中
1、新建一个类库,例如库名为 ResourcesLibrary;
2、添加引用 System.Drawing;
3、添加资源文件(添加--新建项--资源文件),例如文件名为 Resource1.resx;
4、添加图片(打开Resource1.resx,单击“添加资源”后的小三角,选择添加现有文件,选择需要作为资源的图片),例如添加了图片 Sunset.jpg
5、添加类,例如名为 GetImage.cs
7、添加aspx页面,例如名字为GetImage.aspx,Page_Load里添加如下代码
9、按 F5 运行,查看效果
2、添加引用 System.Drawing;
3、添加资源文件(添加--新建项--资源文件),例如文件名为 Resource1.resx;
4、添加图片(打开Resource1.resx,单击“添加资源”后的小三角,选择添加现有文件,选择需要作为资源的图片),例如添加了图片 Sunset.jpg
5、添加类,例如名为 GetImage.cs
using System;
namespace ResourcesLibrary
{
public class GetImage
{
public System.Drawing.Bitmap GetSunset()
{
return Resource1.Sunset;
}
}
}
6、UI层添加对类库的引用(ResourcesLibrary.dll)namespace ResourcesLibrary
{
public class GetImage
{
public System.Drawing.Bitmap GetSunset()
{
return Resource1.Sunset;
}
}
}
7、添加aspx页面,例如名字为GetImage.aspx,Page_Load里添加如下代码
protected void Page_Load(object sender, EventArgs e)
{
ResourcesLibrary.GetImage img = new ResourcesLibrary.GetImage();
System.Drawing.Bitmap bmp = img.GetSunset();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
try
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
}
finally
{
bmp.Dispose();
}
}
8、新建aspx页面,例如名字为ShowIamge.aspx,代码如下;{
ResourcesLibrary.GetImage img = new ResourcesLibrary.GetImage();
System.Drawing.Bitmap bmp = img.GetSunset();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
try
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
}
finally
{
bmp.Dispose();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowIamge.aspx.cs" Inherits="Mick_ShowIamge" %>
<!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>
<asp:Image ID="img1" runat="server" ImageUrl="~/Mick/GetImage.aspx" />
</div>
</form>
</body>
</html>
<!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>
<asp:Image ID="img1" runat="server" ImageUrl="~/Mick/GetImage.aspx" />
</div>
</form>
</body>
</html>
9、按 F5 运行,查看效果