C#实现图片切割、切图、裁剪
作者:霍莉雪特 发布时间:2022-10-24 15:12:19
标签:C#,图片切割,图片裁剪
本文实例为大家分享了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;
}
}
}
运行后的效果如下:
补充:关于剪裁的位置和区域的填写说明,如下图。
来源:https://blog.csdn.net/qq_18995513/article/details/67637521


猜你喜欢
- 前言Feign是Netflix开源的声明式HTTP客户端,致力于让编写http client更加简单,Feign可以通过声明接口自动构造请求
- 目录如何实现简化呢?当然,一些常见的方法已经封装在了 Norns.Urd.Extensions.Polly如何启用 Norns.Urd +
- 写在前面在平时的开发之中,我们需要对于数据加载的情况进行展示:空数据网络异常加载中等等情况现在设置页面状态的方式有多种,由于笔者近期一直在使
- 一、题目描述题目实现:获取远程服务器和客户机的IP地址和端口号。二、解题思路创建一个服务器类:ServerSocketFrame,继承JFr
- 1.for循环import com.google.common.base.Function;import com.google.common
- 一、项目简述功能包括(管理员和用户角色): 酒店预订,酒店管理,员工管理,入住原理,订单管理, 楼层管理,退房管理,营业额报表等等。二、项目
- 缓存,我相信大家对它一定不陌生,在项目中,缓存肯定是必不可少的。市面上有非常多的缓存工具,比如 Redis、Guava Cache 或者 E
- Java操作redis设置第二天凌晨过期场景在做查询数据的时候,遇到了需要设置数据在redis中第二天过期的问题,但是redis又没有对应的
- 1.底层网络接口采用apache的httpclient连接池框架; 2.图片缓存采用基于LRU的算法; 3.网络接口采用监听者模式; 4.包
- /// <summary> /// 导出Ex
- 通过@Query注解支持JPA语句和原生SQL语句在SpringData中们可是使用继承接口直接按照规则写方法名即可完成查询的方法,不需要写
- Android 图片选择可以达到的效果:1.第一个图片的位置放照相机,点击打开照相机2.其余的是显示全部存储的图片,点击一次是查
- 本文实例为大家分享了WinForm实现鼠标拖动控件跟随效果的具体代码,供大家参考,具体内容如下1. 运行初始窗口如下:2. 拖动后效果如下:
- 在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址,一种是设置autolink,让系统自动识别超链接。 代码如下:
- 1,添加依赖在project的build.gradle文件中添加dependencies { classpath 'co
- 对于生成的sql语句 自动加上单引号的情况mybatis是这样的,如果表的字段跟系统字段冲突,写sql语句的时候必须得加上单引号,这样才会区
- 此文通过一段代码来展示java获取相关参数的方法分享给大家:public static void main(String[] args) {
- Command模式是最让我疑惑的一个模式,我在阅读了很多代码后,才感觉隐约掌握其大概原理,我认为理解设计模式最主要是掌握起原理构造,这样才对
- 网上关于下拉刷新的文章也不少,不过都太长了。恰好发现了官方的下拉刷新库,而且效果还是不错的,简洁美观,用得也挺方便。下面是效果图:我的好友原
- RabbitMQ的一些基本组件Producer:消息的生产者Consumer:消息的消费者Broker:MQ服务器,管理队列、消息Messa