Bitmap的Save方法 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; public partial class Bitamap : System.Web.UI.Page { protectedvoid Page_Load(object sender, EventArgs e) { //返回与Web服务器上的制定虚拟路径对应的物理路径 string imagePath = Server.MapPath("sample.jpg"); Bitmap image =new Bitmap(imagePath); System.Drawing.Imaging.ImageFormat imgFormat = image.RawFormat; image.Save(Response.OutputStream, imgFormat); image.Dispose(); } }
二、用DrawImage方法来裁切和缩放图像 常用方法: 在指定位置使用原始物理大小绘制制定的Image对象: public void DrawImage(Image img,Point point);point指定图像左上角坐标。 在指定位置并按指定形状和大小绘制指定的Image对象: public void DrawImage(Image img,Point points);points指定图像形状的多边形。 在指定位置并按指定大小绘制指定的Image对象: public void DrawImage(Image img,Rectangle rect);rect指定边界矩形。 在指定位置并按指定大小绘制制定把的Image对象的指定部分: public void DrawImage(Image img,RectangleF rectfs,RectangleF rectfd,GraphicsUnit unit); rectfs指定源图像中要绘制的区域,rectfd要绘制图像的大小,unit指定用于确定源矩形的度量单位。
DrawImage using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; public partial class DrawImage : System.Web.UI.Page { protectedvoid Page_Load(object sender, EventArgs e) { Bitmap backingimg =new Bitmap(500, 250); Graphics myGraphics = Graphics.FromImage(backingimg); string imagePath = Server.MapPath("sample.jpg"); Bitmap myBitmap =new Bitmap(imagePath); //源图像中要裁切的区域 Rectangle sourceRectangle =new Rectangle(80, 70, 80, 45); //缩小后要绘制的区域 Rectangle destRectangle1 =new Rectangle(300, 10, 20, 16); //放大后绘制到目的区域 Rectangle destRectangle2 =new Rectangle(300, 40, 200, 160); myGraphics.Clear(Color.White); //在点(0,0)绘制原始图像 myGraphics.DrawImage(myBitmap, 0, 0); //绘制缩小的图像 myGraphics.DrawImage(myBitmap, destRectangle1, sourceRectangle, GraphicsUnit.Pixel); //绘制放大的图像 myGraphics.DrawImage(myBitmap, destRectangle2, sourceRectangle, GraphicsUnit.Pixel); backingimg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); backingimg.Dispose(); myBitmap.Dispose(); myGraphics.Dispose(); } }
aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class XY : System.Web.UI.Page { protectedvoid Page_Load(object sender, EventArgs e) { } protectedvoid ImageButton1_Click(object sender, ImageClickEventArgs e) { Label1.Text ="你点击的坐标是:("+ e.X.ToString() +","+ e.Y.ToString() +")"; } }