• 技术文章 >后端开发 >C#.Net教程

    C#图片按比例缩放的示例代码分享

    黄舟黄舟2017-03-16 11:20:02原创1288
    这篇文章主要为大家详细介绍了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 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();
    }

    php入门到就业线上直播课:查看学习

    以上就是C#图片按比例缩放的示例代码分享的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:C#,缩放
    上一篇:C# 7.0之ref locals and returns(局部变量和引用返回)详解 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• c语言中源文件编译后生成什么文件• C#中GDI+编程10个基本技巧二• 应用绝对路径与相对路径• c语言标识符有哪些类型• ASP.NET使用Ajax如何返回Json对象的方法具体介绍
    1/1

    PHP中文网