【C#/WPF】图片的切割/切图/裁剪图片

简介: 原文:【C#/WPF】图片的切割/切图/裁剪图片 前台准备两个Image控件。上面是显示原图,下面显示切割后的效果。 对应的后台代码: public par...
原文: 【C#/WPF】图片的切割/切图/裁剪图片

前台准备两个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;
        }
    }
}

运行后的效果如下:
这里写图片描述

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

目录
相关文章
|
1月前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
6月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
260 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
|
4月前
|
API C#
C# 调用系统“API“设置图片为“桌面壁纸“
C# 调用系统“API“设置图片为“桌面壁纸“
|
6月前
|
C#
C# 图片RGB处理判断
C# 图片RGB处理判断 需要:根据一张原始图的RGB平均值和新的图片的RGB平均值的差距,来判断图中是否出现除原图中物体外的其他物体 前提:.Net framework 4.8 及以上 示例代码: 程序集:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imagin...
21 0
|
9月前
|
人工智能 文字识别 API
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
|
算法 定位技术 C#
C#开发:不规则裁切图片
C#开发:不规则裁切图片
121 0
|
区块链 C#
C#实现把图片转换为ico格式
C#实现把图片转换为ico格式
592 0
|
C# 图形学
C#裁剪图片的方法
C#裁剪图片的方法
255 0
如何在 C#中的listView 控件中显示图片?
如何在 C#中的listView 控件中显示图片?
960 0
如何在 C#中的listView 控件中显示图片?
儿童节教大家做一个C#图片匹配小游戏
儿童节教大家做一个C#图片匹配小游戏
123 0
儿童节教大家做一个C#图片匹配小游戏