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

C#开发 2015-10-24 1388浏览 收藏本文

本文给大家分享一个关于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

转载请注明:图享网 » C#/.NET根据高度和宽度裁剪图片并保存到指定文件目录的示例

验证码