栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

   

    /// 
    /// 保存图片
    /// 
    /// Image 对象
    /// 保存路径
    /// 指定格式的编解码参数
    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();
    }

    /// 
    /// 获取图像编码解码器的所有相关信息
    /// 
    /// 包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串
    /// 返回图像编码解码器的所有相关信息
    private static ImageCodecInfo GetCodecInfo(string mimeType)
    {
      ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
      foreach (ImageCodecInfo ici in CodecInfo)
      {
 if (ici.MimeType == mimeType)
   return ici;
      }
      return null;
    }

    /// 
    /// 计算新尺寸
    /// 
    /// 原始宽度
    /// 原始高度
    /// 最大新宽度
    /// 最大新高度
    /// 
    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);
    }

    /// 
    /// 得到图片格式
    /// 
    /// 文件名称
    /// 
    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;
      }
    }
   

    /// 
    /// 制作小正方形
    /// 
    /// 图片对象
    /// 新地址
    /// 长度或宽度
    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();
      }
    }

    /// 
    /// 制作小正方形
    /// 
    /// 图片文件名
    /// 新地址
    /// 长度或宽度
    public static void MakeSquareImage(string fileName,string newFileName, int newSize)
    {
      MakeSquareImage(Image.FromFile(fileName), newFileName, newSize);
    }
  }
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122785.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号