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

C#正方形图片的绘制方法

程序员文章站 2023-12-10 18:48:52
本文实例为大家分享了c#绘制正方形图片的的具体代码,供大家参考,具体内容如下 using system; using system.collections.ge...

本文实例为大家分享了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 treads
{
  /// <summary>
  /// 制作小正方形
  /// </summary>
  class class3
  {
    private string srcfilename = @"x";//获取图片的路径
    private string srcfilename1 = @"x";//要保持图片的新路径

   

    /// <summary>
    /// 保存图片
    /// </summary>
    /// <param name="image">image 对象</param>
    /// <param name="savepath">保存路径</param>
    /// <param name="ici">指定格式的编解码参数</param>
    private static void saveimage(image image, string savepath, imagecodecinfo ici)
    {
      //设置 原图片 对象的 encoderparameters 对象
      encoderparameters parameters = new encoderparameters(1);
      parameters.param[0] = new encoderparameter(system.drawing.imaging.encoder.quality, ((long)100));
      image.save(savepath, ici, parameters);
      parameters.dispose();
    }

    /// <summary>
    /// 获取图像编码解码器的所有相关信息
    /// </summary>
    /// <param name="mimetype">包含编码解码器的多用途网际邮件扩充协议 (mime) 类型的字符串</param>
    /// <returns>返回图像编码解码器的所有相关信息</returns>
    private static imagecodecinfo getcodecinfo(string mimetype)
    {
      imagecodecinfo[] codecinfo = imagecodecinfo.getimageencoders();
      foreach (imagecodecinfo ici in codecinfo)
      {
        if (ici.mimetype == mimetype)
          return ici;
      }
      return null;
    }

    /// <summary>
    /// 计算新尺寸
    /// </summary>
    /// <param name="width">原始宽度</param>
    /// <param name="height">原始高度</param>
    /// <param name="maxwidth">最大新宽度</param>
    /// <param name="maxheight">最大新高度</param>
    /// <returns></returns>
    private static size resizeimage(int width, int height, int maxwidth, int maxheight)
    {
      //此次2012-02-05修改过=================
      if (maxwidth <= 0)
        maxwidth = width;
      if (maxheight <= 0)
        maxheight = height;
      //以上2012-02-05修改过=================
      decimal max_width = (decimal)maxwidth;
      decimal max_height = (decimal)maxheight;
      decimal aspect_ratio = max_width / max_height;

      int newwidth, newheight;
      decimal originalwidth = (decimal)width;
      decimal originalheight = (decimal)height;

      if (originalwidth > max_width || originalheight > max_height)
      {
        decimal factor;
        // determine the largest factor 
        if (originalwidth / originalheight > aspect_ratio)
        {
          factor = originalwidth / max_width;
          newwidth = convert.toint32(originalwidth / factor);
          newheight = convert.toint32(originalheight / factor);
        }
        else
        {
          factor = originalheight / max_height;
          newwidth = convert.toint32(originalwidth / factor);
          newheight = convert.toint32(originalheight / factor);
        }
      }
      else
      {
        newwidth = width;
        newheight = height;
      }
      return new size(newwidth, newheight);
    }

    /// <summary>
    /// 得到图片格式
    /// </summary>
    /// <param name="name">文件名称</param>
    /// <returns></returns>
    public static imageformat getformat(string name)
    {
      string ext = name.substring(name.lastindexof(".") + 1);
      switch (ext.tolower())
      {
        case "jpg":
        case "jpeg":
          return imageformat.jpeg;
        case "bmp":
          return imageformat.bmp;
        case "png":
          return imageformat.png;
        case "gif":
          return imageformat.gif;
        default:
          return imageformat.jpeg;
      }
    }
   

    /// <summary>
    /// 制作小正方形
    /// </summary>
    /// <param name="image">图片对象</param>
    /// <param name="newfilename">新地址</param>
    /// <param name="newsize">长度或宽度</param>
    public static void makesquareimage(image image, string newfilename, int newsize)
    {
      int i = 0;
      int width = image.width;
      int height = image.height;
      if (width > height)
        i = height;
      else
        i = width;

      bitmap b = new bitmap(newsize, newsize);

      try
      {
        graphics g = graphics.fromimage(b);
        //设置高质量插值法
        g.interpolationmode = interpolationmode.highqualitybicubic;
        //设置高质量,低速度呈现平滑程度
        g.smoothingmode = smoothingmode.antialias;
        g.pixeloffsetmode = pixeloffsetmode.highquality;
        //清除整个绘图面并以透明背景色填充
        g.clear(color.transparent);
        if (width < height)
          g.drawimage(image, new rectangle(0, 0, newsize, newsize), new rectangle(0, (height - width) / 2, width, width), graphicsunit.pixel);
        else
          g.drawimage(image, new rectangle(0, 0, newsize, newsize), new rectangle((width - height) / 2, 0, height, height), graphicsunit.pixel);

        saveimage(b, newfilename, getcodecinfo("image/" + getformat(newfilename).tostring().tolower()));
      }
      finally
      {
        image.dispose();
        b.dispose();
      }
    }

    /// <summary>
    /// 制作小正方形
    /// </summary>
    /// <param name="filename">图片文件名</param>
    /// <param name="newfilename">新地址</param>
    /// <param name="newsize">长度或宽度</param>
    public static void makesquareimage(string filename,string newfilename, int newsize)
    {
      makesquareimage(image.fromfile(filename), newfilename, newsize);
    }
  }
}

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