欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#图片按比例缩放实例

程序员文章站 2023-11-22 13:39:52
本文实例为大家分享了c#图片按比例缩放的具体代码,供大家参考,具体内容如下 工具类代码: using system; using system.col...

本文实例为大家分享了c#图片按比例缩放的具体代码,供大家参考,具体内容如下

工具类代码:

using system;
using system.collections.generic;
using system.drawing;
using system.drawing.drawing2d;
using system.drawing.imaging;
using system.linq;
using system.text;
using system.threading.tasks;

namespace zoomimage.utils
{
 /// <summary>
 /// 图片缩放
 /// </summary>
 public class zoomimageutil
 {
  /// <summary>
  /// 图片缩放
  /// </summary>
  /// <param name="bmp">图片</param>
  /// <param name="width">目标宽度,若为0,表示宽度按比例缩放</param>
  /// <param name="height">目标长度,若为0,表示长度按比例缩放</param>
  public static bitmap getthumbnail(bitmap bmp, int width, int height)
  {
   if (width == 0)
   {
    width = height * bmp.width / bmp.height;
   }
   if (height == 0)
   {
    height = width * bmp.height / bmp.width;
   }

   image imgsource = bmp;
   bitmap outbmp = new bitmap(width, height);
   graphics g = graphics.fromimage(outbmp);
   g.clear(color.transparent);
   // 设置画布的描绘质量   
   g.compositingquality = compositingquality.highquality;
   g.smoothingmode = smoothingmode.highquality;
   g.interpolationmode = interpolationmode.highqualitybicubic;
   g.drawimage(imgsource, new rectangle(0, 0, width, height + 1), 0, 0, imgsource.width, imgsource.height, graphicsunit.pixel);
   g.dispose();
   imgsource.dispose();
   bmp.dispose();
   return outbmp;
  }
 }
}

使用示例:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.io;
using system.linq;
using system.text;
using system.threading;
using system.threading.tasks;
using system.windows.forms;
using zoomimage.utils;

namespace zoomimage
{
 public partial class form1 : form
 {
  public form1()
  {
   initializecomponent();
  }

  private void form1_load(object sender, eventargs e)
  {
   openfiledialog1.multiselect = true;
  }

  private void txtwidth_keypress(object sender, keypresseventargs e)
  {
   if (e.keychar != 8 && !char.isdigit(e.keychar))
   {
    e.handled = true;
   }
  }

  private void txtheight_keypress(object sender, keypresseventargs e)
  {
   if (e.keychar != 8 && !char.isdigit(e.keychar))
   {
    e.handled = true;
   }
  }

  private void btnselectimage_click(object sender, eventargs e)
  {
   try
   {
    if (txtwidth.text == "" && txtheight.text == "")
    {
     return;
    }

    if (openfiledialog1.showdialog() == dialogresult.ok)
    {
     task.factory.startnew(() =>
     {
      string path = path.getdirectoryname(openfiledialog1.filenames[0]) + "\\newimage\\";

      int i = 0;
      foreach (string filename in openfiledialog1.filenames)
      {
       bitmap bmp = zoomimageutil.getthumbnail(new bitmap(filename), convert.toint32(txtwidth.text == "" ? "0" : txtwidth.text), convert.toint32(txtheight.text == "" ? "0" : txtheight.text));
       if (!directory.exists(path))
       {
        directory.createdirectory(path);
       }
       file.delete(path + path.getfilename(filename));
       bmp.save(path + path.getfilename(filename));
       this.invoke(new invokedelegate(() =>
       {
        lblprogress.text = string.format("进度:{1}/{0}", openfiledialog1.filenames.length, ++i);
       }));
       thread.sleep(1);
      }

      messagebox.show("成功!");
     });
    }
   }
   catch (exception ex)
   {
    messagebox.show(ex.message);
   }
  }

 }

 /// <summary>
 /// 跨线程访问控件的委托
 /// </summary>
 public delegate void invokedelegate();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。