首页 / C# / 正文

C#/.NET根据高度和宽度裁剪图片并保存到指定文件目录的示例

5507 发布于: 2015-10-24 读完约需10分钟

概述

本文给大家分享一个关于C#/.NET应用程序中根据高度和宽度裁剪图片并保存到指定文件目录的简单示例,在很多项目开发需求中,对图片的裁剪操作还是比较常见的。

不多说,我们直接进入今天这篇文章的主题吧。

本示例主要用到的.NET命名空间有:

System.Drawing;
System.Drawing.Drawing2D;
System.Drawing.Imaging;
System.IO;

以下是将指定图片文件按照指定宽,高转换成byte[]的静态方法:

static byte[] CropImage(string imagePath, int x, int y, int width, int height)
    {
      try
      {
        using (var originalImg = Image.FromFile(imagePath))
        {
          using (var bmp = new Bitmap(width, height))
          {
            bmp.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution);
            using (var graphic = Graphics.FromImage(bmp))
            {
              graphic.SmoothingMode = SmoothingMode.AntiAlias;
              graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
              graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
              graphic.DrawImage(originalImg, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
              var ms = new MemoryStream();
              bmp.Save(ms, originalImg.RawFormat);
              return ms.GetBuffer();
            }
          }
        }
      }
      catch (Exception ex)
      {
        throw ex;
      }
    }

以下是将图片的btye[]数据转换保存到指定文件目录的静态方法代码:

static void SaveByteArrayToImageFile(byte[] imageByte)
    {
      try
      {
        using (var image = Image.FromStream(new MemoryStream(imageByte)))
        {
          image.Save(@"crop-image-demo-after.jpg", ImageFormat.Jpeg);
        }
      }
      catch(Exception ex)
      {
        throw ex;
      }
    }

以下贴出本示例的完整代码:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
using System;

namespace CropImageDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Starting crop image...");
      SaveByteArrayToImageFile(CropImage(@"crop-image-demo-before.jpg", 0, 0, 240, 240));
      Console.WriteLine("Crop image completed.");
    }

    static byte[] CropImage(string imagePath, int x, int y, int width, int height)
    {
      try
      {
        using (var originalImg = Image.FromFile(imagePath))
        {
          using (var bmp = new Bitmap(width, height))
          {
            bmp.SetResolution(originalImg.HorizontalResolution, originalImg.VerticalResolution);
            using (var graphic = Graphics.FromImage(bmp))
            {
              graphic.SmoothingMode = SmoothingMode.AntiAlias;
              graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
              graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
              graphic.DrawImage(originalImg, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
              var ms = new MemoryStream();
              bmp.Save(ms, originalImg.RawFormat);
              return ms.GetBuffer();
            }
          }
        }
      }
      catch (Exception ex)
      {
        throw ex;
      }
    }

    static void SaveByteArrayToImageFile(byte[] imageByte)
    {
      try
      {
        using (var image = Image.FromStream(new MemoryStream(imageByte)))
        {
          image.Save(@"crop-image-demo-after.jpg", ImageFormat.Jpeg);
        }
      }
      catch(Exception ex)
      {
        throw ex;
      }
    }
  }
}

以下是本文的示例图片裁剪前后的效果。

裁剪前:

0_9_0_summary

裁剪后:

crop-image-demo-after

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

上一篇: 基于ASP.NET(C#)[支持MVC和Webform]的免费开源灵活的CMS管理系统--Kaliko

下一篇: 支持Ajax跨域访问ASP.NET Web Api 2(Cors)的简单示例教程演示

本文永久链接码友网 » C#/.NET根据高度和宽度裁剪图片并保存到指定文件目录的示例

分享扩散:

发表评论

登录用户才能发表评论, 请 登 录 或者 注册