实现Bitmap和BitmapSource之间的转换

本文介绍了一个实用的图像处理工具类ImageUtils,该类提供了多种图像转换功能,包括将InkCanvas笔迹转化为BitmapSource图像,Bitmap与BitmapSource之间的相互转换,以及Bitmap与字节数组之间的转换。这些功能在WPF应用中特别有用,可以方便地处理和保存用户绘制的图像。

转载: http://www.firstsolver.com/wordpress/?p=3462

/* ----------------------------------------------------------
 * 文件名称:ImageUtils.cs
 *
 * 作者:秦建辉
 *
 * 微信:splashcn
 *
 * 博客:http://www.firstsolver.com/wordpress/
 *
 * 开发环境:
 *      Visual Studio V2017
 *      .NET Framework 4 Client Profile
 *
 * 版本历史:
 *      V1.0    2018年01月03日
 *              实现Bitmap和BitmapSource之间的转换
 * ------------------------------------------------------------ */
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;
  
namespace Com.FirstSolver.Splash
{
    public static class ImageUtils
    {
        /// <summary>
        /// 将 InkCanvas 笔迹 转化为 BitmapSource 图像
        /// </summary>
        /// <param name="canvas"/>InkCanvas 控件
        /// <returns>存储笔画图像的 BitmapSource</returns>
        public static BitmapSource ToBitmapSource(this System.Windows.Controls.InkCanvas canvas)
        {
            // 获取笔画边界
            System.Windows.Rect rect = canvas.Strokes.GetBounds();
  
            // 获取笔画轮廓几何图形
            double width = canvas.DefaultDrawingAttributes.Width; // 笔画宽度
            double height = canvas.DefaultDrawingAttributes.Height; // 笔画高度
            System.Windows.Media.RectangleGeometry geometry = new System.Windows.Media.RectangleGeometry(new System.Windows.Rect(rect.X - width / 2, rect.Y - height / 2, rect.Width + width, rect.Height + height));
            canvas.Clip = geometry;
            canvas.UpdateLayout();
  
            // 将笔画转换为图像
            RenderTargetBitmap bitmap = new RenderTargetBitmap((int)geometry.Rect.Width, (int)geometry.Rect.Height, 96, 96, System.Windows.Media.PixelFormats.Default);
            System.Windows.Media.DrawingVisual visual = new System.Windows.Media.DrawingVisual();
            using (System.Windows.Media.DrawingContext context = visual.RenderOpen())
            {
                context.DrawRectangle(new System.Windows.Media.VisualBrush(canvas), null, new System.Windows.Rect(0, 0, geometry.Rect.Width, geometry.Rect.Height));
            }
            bitmap.Render(visual);
            canvas.Clip = null;
            return bitmap;
        }
  
        /// <summary>
        /// 将 Bitmap 转化为 BitmapSource
        /// </summary>
        /// <param name="bmp"/>要转换的 Bitmap
        /// <returns>转换后的 BitmapSource</returns>
        public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap bmp)
        {
            System.IntPtr hBitmap = bmp.GetHbitmap();
            try
            {
                return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, System.IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                DeleteObject(hBitmap);
            }
        }
  
        /// <summary>
        /// 将 Bitmap 转化为 BitmapSource
        /// </summary>
        /// <param name="bmp"/>要转换的 Bitmap
        /// <returns>转换后的 BitmapImage</returns>
        public static BitmapImage ToBitmapImage(this System.Drawing.Bitmap bmp)
        {            
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmp.Save(ms, ImageFormat.Bmp);
  
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = ms;
            image.EndInit();
            return image;
        }
 
        /// <summary>
        /// 将字节数组转换为 BitmapImage
        /// </summary>
        /// <param name="bytes">要转换的字节数组</param>
        /// <returns>转换后的 BitmapImage</returns>
        public static BitmapImage ToBitmapImage(this byte[] bytes)
        {
            BitmapImage image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = new System.IO.MemoryStream(bytes);
            image.EndInit();
            return image;
        }
 
        /// <summary>
        /// 将 Bitmap 转化为字节数组
        /// </summary>
        /// <param name="bmp">要转换的 Bitmap</param>
        /// <returns>转换后的字节数组</returns>
        public static byte[] ToByteArray(this System.Drawing.Bitmap bmp)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                bmp.Save(ms, ImageFormat.Bmp);
                return ms.ToArray();
            }
        }
  
        /// <summary>
        /// 将 BitmapSource 转化为 Bitmap
        /// </summary>
        /// <param name="source"/>要转换的 BitmapSource
        /// <returns>转化后的 Bitmap</returns>
        public static System.Drawing.Bitmap ToBitmap(this BitmapSource source)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                BitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(ms);
                return new System.Drawing.Bitmap(ms);
            }
        }
  
        [System.Runtime.InteropServices.DllImport("Gdi32.dll")]
        private static extern bool DeleteObject(System.IntPtr hObject);
    }
}

 

内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程,应动手调试参数,复现文翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值