温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-05-17 10:25:25 来源:亿速云 阅读:1397 作者:小新 栏目:编程语言

这篇文章主要介绍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#如何实现图片切割、切图、裁剪

C#是什么

C#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的首选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。

以上是“C#如何实现图片切割、切图、裁剪”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI