C#实现图片切割、切图、裁剪

作者:霍莉雪特 时间:2022-10-24 15:12:19 

本文实例为大家分享了C#实现图片切割、切图的具体代码,供大家参考,具体内容如下

前台准备两个Image控件。上面是显示原图,下面显示切割后的效果。


<StackPanel Orientation="Vertical">
<Image Width="450" Height="383" Source="C:\Users\Administrator\Documents\Visual Studio 2015\Projects\SplitPic\SplitPic\Images\1.jpg"/>
<Image x:Name="img" Stretch="None" Width="450" Height="383" />
</StackPanel>

对应的后台代码:


public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

// 设置原图
img.Source = new BitmapImage(new Uri(@"Images/1.jpg", UriKind.Relative));

// 切割图片
ImageSource imageSource = img.Source;
Bitmap bitmap = SystemUtils.ImageSourceToBitmap(imageSource);
BitmapSource bitmapSource = SystemUtils.BitmapToBitmapImage(bitmap);
BitmapSource newBitmapSource = SystemUtils.CutImage(bitmapSource, new Int32Rect(125, 60, 235, 285));

// 使用切割后的图源
img.Source = newBitmapSource;
}

}

// 图像工具类
public static class SystemUtils
{
/// <summary>
/// 切图
/// </summary>
/// <param name="bitmapSource">图源</param>
/// <param name="cut">切割区域</param>
/// <returns></returns>
public static BitmapSource CutImage(BitmapSource bitmapSource, Int32Rect cut)
{
//计算Stride
var stride = bitmapSource.Format.BitsPerPixel * cut.Width / 8;
//声明字节数组
byte[] data = new byte[cut.Height * stride];
//调用CopyPixels
bitmapSource.CopyPixels(cut, data, stride, 0);

return BitmapSource.Create(cut.Width, cut.Height, 0, 0, PixelFormats.Bgr32, null, data, stride);
}

// ImageSource --> Bitmap
public static System.Drawing.Bitmap ImageSourceToBitmap(ImageSource imageSource)
{
BitmapSource m = (BitmapSource)imageSource;

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m.PixelWidth, m.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

System.Drawing.Imaging.BitmapData data = bmp.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

m.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bmp.UnlockBits(data);

return bmp;
}

// Bitmap --> BitmapImage
public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
 bitmap.Save(stream, ImageFormat.Bmp);

stream.Position = 0;
 BitmapImage result = new BitmapImage();
 result.BeginInit();
 // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed."
 // Force the bitmap to load right now so we can dispose the stream.
 result.CacheOption = BitmapCacheOption.OnLoad;
 result.StreamSource = stream;
 result.EndInit();
 result.Freeze();

return result;
}
}
}

运行后的效果如下:

C#实现图片切割、切图、裁剪

补充:关于剪裁的位置和区域的填写说明,如下图。

C#实现图片切割、切图、裁剪

来源:https://blog.csdn.net/qq_18995513/article/details/67637521

标签:C#,图片切割,图片裁剪
0
投稿

猜你喜欢

  • .net(c#)中的new关键字详细介绍

    2021-08-29 21:19:27
  • Java实现文本编译器

    2022-10-21 18:33:20
  • 浅析Java编程中枚举类型的定义与使用

    2021-07-04 23:46:16
  • Java Validation Api使用方法实例解析

    2023-05-16 05:44:58
  • 教你如何使用Java输出各种形状

    2023-08-21 00:46:55
  • 基于java构造方法Vector创建对象源码分析

    2023-11-25 11:27:54
  • springcloud-gateway整合jwt+jcasbin实现权限控制的详细过程

    2023-11-20 12:57:09
  • Android实战教程第五篇之一键锁屏应用

    2023-12-03 18:47:36
  • Android实现中国象棋附源码下载

    2023-12-20 17:09:10
  • Java静态泛型使用方法实例解析

    2023-03-31 00:41:35
  • Android组合控件自定义标题栏

    2021-11-04 01:12:36
  • C# 实现颜色的梯度渐变案例

    2023-11-20 22:01:06
  • springMVC自定义注解,用AOP来实现日志记录的方法

    2023-11-29 13:58:53
  • Android游戏开发:实现手势操作切换图片的实例

    2022-05-06 11:55:01
  • Android中检查、监听电量和充电状态的方法

    2023-05-15 23:23:19
  • C#微信公众号开发之消息处理

    2023-11-10 01:10:53
  • Flutter持久化存储之数据库存储(sqflite)详解

    2022-11-16 04:34:30
  • Android中的Launch Mode详情

    2022-10-06 02:38:09
  • JAVA算法起步之插入排序实例

    2021-05-30 15:52:30
  • 深入理解Java注解类型(@Annotation)

    2022-11-14 17:28:42
  • asp之家 软件编程 m.aspxhome.com