首页 > 开发 > 综合 > 正文

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

2024-07-21 02:26:11
字体:
来源:转载
供稿:网友

代码段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
 }
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表