网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#正方形图片怎么绘制-创新互联

小编给大家分享一下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);
    }
  }
}

C#是什么

C#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。

以上是“C#正方形图片怎么绘制”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都网站设计公司行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:C#正方形图片怎么绘制-创新互联
URL标题:http://bjjierui.cn/article/ijogg.html

其他资讯