C#图片按比例缩放实例

 更新时间:2017年03月11日 09:38:14   作者:秋荷雨翔  
这篇文章主要为大家详细介绍了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();
}

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

相关文章

  • C#中backgroundWorker类的用法详解

    C#中backgroundWorker类的用法详解

    这篇文章主要介绍了C#使用backgroundWorker实现多线程的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • C# 常用不同日志库的区别与示例解析

    C# 常用不同日志库的区别与示例解析

    C# 作为一种流行的编程语言,有许多优秀的日志库可供选择,本文将介绍一些常见的 C# 日志库,包括 NLog、log4net、Serilog 和 Microsoft.Extensions.Logging,并通过示例展示它们的使用方法及区别,感兴趣的朋友一起看看吧
    2024-06-06
  • 详解Asp.Net MVC的Bundle捆绑

    详解Asp.Net MVC的Bundle捆绑

    这篇文章主要介绍了Asp.Net MVC的Bundle捆绑方法,具体实现方法给大家做代码整理,一起参考一下。
    2017-11-11
  • Parallel.For循环与普通for循环的性能比较

    Parallel.For循环与普通for循环的性能比较

    这篇文章介绍了Parallel.For循环与普通for循环的性能比较,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • Unity实现植物识别示例详解

    Unity实现植物识别示例详解

    这篇文章主要介绍了如何通过Unity接入百度AI实现植物识别,接口返回植物的名称,并支持获取识别结果对应的百科信息。感兴趣的可以了解一下
    2022-01-01
  • C#获取指定年份第一个星期一具体日期的方法

    C#获取指定年份第一个星期一具体日期的方法

    这篇文章主要介绍了C#获取指定年份第一个星期一具体日期的方法,涉及C#日期与字符串操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C#实现将汉字转化为2位大写的16进制Unicode的方法

    C#实现将汉字转化为2位大写的16进制Unicode的方法

    这篇文章主要介绍了C#实现将汉字转化为2位大写的16进制Unicode的方法,分析了转换的技巧并以实例形式给出了具体的转换方法,非常具有实用价值,需要的朋友可以参考下
    2014-12-12
  • c#中@的3种作用

    c#中@的3种作用

    本文主要介绍了c#中@的3种作用。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • 浅析wpf中datagrid显示列的问题

    浅析wpf中datagrid显示列的问题

    这篇文章主要为大家详细介绍了wpf中datagrid显示列问题的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以学习一下
    2024-04-04
  • .NET WinForm实现在listview中添加progressbar的方法

    .NET WinForm实现在listview中添加progressbar的方法

    这篇文章主要介绍了.NET WinForm实现在listview中添加progressbar的方法,结合实例形式简单分析了进度条控件的添加与使用方法,需要的朋友可以参考下
    2017-05-05

最新评论