关于system.drawing.imaging类的说明和范例

by 陈少俊 2009.12.11 21:48

代码段1
=========================
System.Drawing.Image image =
System.Drawing.Image.FromFile(originalFilename);
System.Drawing.Size size = GetImageSize(image.Width, image.Height,
information.MaximumDimension);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,
size.Height);
System.Drawing.Graphics graphics =
System.Drawing.Graphics.FromImage(bitmap);
graphics.IntERPolationMode =
System.Drawing.Drawing2D.InterpolationMode.High;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.Clear(information.BackgroundColor);
graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width,
bitmap.Height), new System.Drawing.Rectangle(0, 0, image.Width,
image.Height), System.Drawing.GraphicsUnit.Pixel);
graphics.Dispose();

代码段2
============================
//原始图片名称
string originalFilename = "c:\\222.jpg";
//生成的高质量图片名称
string strGoodFile = "c:\\222-small-good.jpg";
//生成的低质量图片名称
string strBadFile = "c:\\222-small-bad.jpg";
//缩小的倍数
int iScale = 3;

//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
//取得图片大小
System.Drawing.Size size = new Size(image.Width / iScale , image.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//取得原图像的普通缩略图
System.Drawing.Image img = image.GetThumbnailImage(image.Width / iScale, image.Height / iScale, null, IntPtr.Zero);
//保存普通缩略图
img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);

g.Dispose();
MessageBox.Show("生成完毕");

俺自己写的一段原图+缩略图代码
===============================
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace BSCRM.Shangpin
{
/// <summary>
/// Shangpin_IMG 的摘要说明。
/// </summary>
public class Shangpin_IMG : System.Web.UI.Page
{
  string ImagePath = ConfigurationSettings.AppSettings["ImagePath"]+"\\";
  bool error;
  string src;
  int height,width;
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   height=width=0;

   if(Request["h"]!=null&&Request["h"].ToString().Trim()!="")
   {
    height=int.Parse(Request["h"].ToString().Trim());
   }
   if(Request["w"]!=null&&Request["h"].ToString().Trim()!="")
   {
    width=int.Parse(Request["w"].ToString().Trim());
   }

   if(Request["src"]!=null)
   {
    src = ImagePath+Request["src"].ToString();
    try
    {
     System.Drawing.Image img = System.Drawing.Image.FromFile(src);

     if(height>0||width>0)
     {
      if(height==0) height=img.Height;
      if(width==0) width=img.Width;
      System.Drawing.Image img2 = img.GetThumbnailImage(width,height,null,IntPtr.Zero);
      img2.Save(Response.OutputStream,img.RawFormat);
      img2.Dispose();
     }
     else
     {
         img.Save(Response.OutputStream,img.RawFormat);
     }

     img.Dispose();
    }
    catch
    {
     error = true;
    }
   }
   else
   {
    error = true;
   }

   if(error)
   {
    Response.StatusCode = 404;
    Response.Write("<html><head><title>Object Not Found</title></head><body><h1>HTTP/1.1 404 Object Not Found</h1></body></html>");
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.net Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {  
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
}
}

Tags:

Asp.Net技术 | web开发

博客评论由 Disqus 驱动

关于博主

博主——陈少俊

   曾经的我们并不相识,但是网络让我们联系了起来,人生本来就是这样,有许多的必然和偶然,我不能做太多,只 是分享我自己拥有的,看到的,听到的。我致力于网站开发,系统建设,推广自己,谢谢大家的关心和爱护。
现就职于汽车时尚传媒有限公司网络部,中国汽车时尚网:http://www.86che.com/
汽车时尚网分类说明:汽车时尚网分类中所有新闻来源于中国汽车时尚网
汽车时尚报分类说明:汽车时尚报分类中所有新闻均为汽车时尚报出刊报纸中所有内容。
汽车时尚报电子版从《汽车时尚报》第493期开始正式免费上线,具体新闻内容请查阅【汽车时尚报】分类。
联系电话:13551107754,(028)68116678
MSN:chen1989jun@hotmail.com

日历中查看

<<  February 2012  >>
MonTueWedThuFriSatSun
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

切换到大日历中浏览

按月阅读

最近的评论

评论 RSS

声明

本博所有网友评论不代表本博立场,版权归其作者所有。

© Copyright 2009-2010  All Rights Piao Blog
蜀ICP备09038546号

部件 Statistics 未找到.

There is an error in XML document (0, 0).X