直接从文件读取出来的图片跟显示的控件大小并不成比例,如果在窗体编程中直接让PictureBox来显示就会按PictureBox的大小截断显示,会丢失部分图片或控件部分空白;如果在网页编程中直接让Image控件或ImageButton控件显示就会按控件的比例填满,造成图片变形。
下面讲讲在两种方式编程中既不会令图片部分丢失又不会令图片变形的方法。
窗体编程比较简单了,只要写一个函数把图片按长宽转换一下,再把转换后的图片显示在控件上就OK了,函数定义如下:
///
<summary>
///
按比例缩放图片
///</summary>
///<param name="SourceImage"></param>
///<param name="TargetWidth"></param>
///<param name="TargetHeight"></param>
private Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight)
{
int IntWidth; //
新的图片宽
int IntHeight; //
新的图片高
try
{
System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat;
System.Drawing.Bitmap SaveImage = new System.Drawing.Bitmap(TargetWidth, TargetHeight);
Graphics g = Graphics.FromImage(SaveImage);
g.Clear(Color.White);
//
计算缩放图片的大小
if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//
宽度比目的图片宽度大,长度比目的图片长度小
{
IntWidth = TargetWidth;
IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
}
else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//
宽度比目的图片宽度小,长度比目的图片长度大
{
IntHeight = TargetHeight;
IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
}
else//
长宽比目的图片长宽小或大
{
IntWidth = TargetWidth;
IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
if (IntHeight > TargetHeight)//
重新计算
{
IntHeight = TargetHeight;
IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
}
}
g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight);
SourceImage.Dispose();
return SaveImage;
}
catch (Exception ex)
{
throw ex;
}
}
从磁盘读取图片到内存,并显示在控件pictureBox1上:
Image
im = Image.FromFile(FileName);
this.pictureBox1.Image = this.ZoomPicture(im,this.pictureBox1.Width,pictureBox1.Height);
当然使用PictureBox的SizeMode属性令它等于PictureBoxSizeMode.Zoom可以达到同样的显示效果,但是如果将图片存储到数据库什么的,就可以通过这个函数来转换后在存储,使所有图片的大小一致。
不过要在网页里显示按比例缩放的图片就比较复杂了,首先新建一个叫DisplayImage.aspx 的WebForm,编辑函数:
protected
void Page_Load(object sender, EventArgs e)
{
try
{
int nWidth, nHeight;
string ImagePath = Page.Request.QueryString["ImagePath"];
string strwidth = Page.Request.QueryString["Width"];
string strheight = Page.Request.QueryString["Height"];
try
{
if (strwidth == string.Empty || strheight == string.Empty)
{
nWidth = 150;
nHeight = 150;
}
else
{
nWidth = Convert.ToInt32(strwidth);
nHeight = Convert.ToInt32(strheight);
}
}
catch
{
nWidth = 150;
nHeight = 150;
}
ImageManager.DisplayPicture(Server.MapPath(ImagePath), nWidth, nHeight,Page.Response);
}
catch
{
}
}
函数DisplayPicture是ImageManager的一个静态方法,在这个函数计算图片的缩放并把文件输出到网页,其定义如下:
///
<summary>
///
按指定长宽显示指定路径的图片
///</summary>
///<param name="SourceImagestr"></param>
///<param name="TargetWidth"></param>
///<param name="TargetHeight"></param>
public static void DisplayPicture(string SourceImagestr, int TargetWidth, int TargetHeight, HttpResponse Response)
{
int IntWidth; //
新的图片宽
int IntHeight; //
新的图片高
System.Drawing.Image SourceImage; //
来源图片定义
try
{
SourceImage = System.Drawing.Image.FromFile(SourceImagestr);
System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat;
System.Drawing.Bitmap SaveImage = new System.Drawing.Bitmap(TargetWidth, TargetHeight);
Graphics g = Graphics.FromImage(SaveImage);
g.Clear(Color.White);
//
计算缩放图片的大小
if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//
宽度比目的图片宽度大,长度比目的图片长度小
{
IntWidth = TargetWidth;
IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
}
else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//
宽度比目的图片宽度小,长度比目的图片长度大
{
IntHeight = TargetHeight;
IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
}
else//
长宽比目的图片长宽小或大
{
IntWidth = TargetWidth;
IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
if (IntHeight > TargetHeight)//
重新计算
{
IntHeight = TargetHeight;
IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
}
}
g.DrawImage(SourceImage, (TargetWidth - IntWidth) / 2, (TargetHeight - IntHeight) / 2, IntWidth, IntHeight);
SourceImage.Dispose();
System.IO.MemoryStream ms = new System.IO.MemoryStream();
SaveImage.Save(ms, format);
Response.ClearContent();
Response.ContentType = "image/"+format.ToString();
Response.BinaryWrite(ms.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
在要显示图片的网页加一个Image或ImageButton控件,假设已加的控件是ImageButton1,代码为:
<
asp
:
ImageButton
ID="ImageButton1" ImageUrl='<%# "DisplayImage.aspx?ImagePath=~/Upload/MyPicture.gif&Width=100&Height=100" %>' runat="server" Height="100px" Width="100px" />
这样在这个ImageButton控件里就可以显示按图片比例大小缩放后能在控件显示的最大图片了,Image控件使用方法相同,如果显示图片是从服务端代码获取只要把ImageUrl='<%# "DisplayImage.aspx?ImagePath=~/Upload/MyPicture.gif&Width=100&Height=100" %>'改成ImageUrl='<%# "DisplayImage.aspx?ImagePath=
"
+ Eval("PicAddress") +
"
&Width=100&Height=100" %>'
就可以了。
图片显示的大小参数可以自己设置。