zoukankan      html  css  js  c++  java
  • 测试Bitmap和Marshal.Copy

     #region 测试Bitmap和Marshal.Copy
            public static void TestBitmapAndMarshalCopy()
            {
                //测试Bitmap和Marshal.Copy
                Bitmap bitmap1 = new Bitmap("./123456.jpg");
                Rectangle rectangle = new Rectangle(0, 0, bitmap1.Width, bitmap1.Height);
                //锁定bitmap到系统内存
                System.Drawing.Imaging.BitmapData bitmapData = bitmap1.LockBits(rectangle, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap1.PixelFormat);
                //BitmapData 指定 Bitmap 的特性,如大小、像素格式、像素数据在内存中的起始地址以及每个扫描行的长度(步幅)。
                var pixel = bitmapData.PixelFormat; //Bitmap 的特性 宽度
                var Width = bitmapData.Width;//Bitmap 的特性 宽度
                var Height = bitmapData.Height; //Bitmap 的特性 高度
                var firstStartAddress = bitmapData.Scan0;//像素数据在内存中的起始地址
                var Stride = bitmapData.Stride;//像素数据在内存中的每个扫描行的长度(步幅)
                var leength = Math.Abs(bitmapData.Stride) * bitmapData.Height;
                byte[] bts = new byte[leength];
                // 将数据从非托管内存指针复制到托管 8 位无符号整数数组
                System.Runtime.InteropServices.Marshal.Copy(firstStartAddress, bts, 0, bts.Length);
                //使rgb图片变红色
                for (int i = 2; i < bts.Length; i += 3)
                {
                    bts[i] = 255;
                }
                //将数据从一维托管 8 位无符号整数数组复制到非托管内存指针。
                System.Runtime.InteropServices.Marshal.Copy(bts, 0, firstStartAddress, bts.Length);
                bitmap1.UnlockBits(bitmapData);
    
                bitmap1.Save("./123456new.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                bitmap1.Dispose();
    
                //测试托管内存到非托管内存
                string a = "123456";
                IntPtr dd = System.Runtime.InteropServices.Marshal.AllocHGlobal(10);
                char[] chars = a.ToCharArray();
                // 将数据从一维托管字符数组复制到非托管内存指针。
                System.Runtime.InteropServices.Marshal.Copy(chars, 0, dd, chars.Length);
                //修改
                //
                char[] chars1 = new char[chars.Length];
                //将数据从非托管内存指针复制到托管字符数组。
                System.Runtime.InteropServices.Marshal.Copy(dd, chars1, 0, chars.Length);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(dd);
            }
            #endregion
    龙腾一族至尊龙骑
  • 相关阅读:
    Tomcat8 配置Oracle11g数据源
    通过代码设置 为横屏
    HttpClient4.3.6 实现https访问
    创建mysql数据库
    设置android状态栏颜色和toolbar颜色一致
    定义任务打印gradle下载的jar包位置
    修改weblogic jvm启动参数
    android.support.v7.widget.Toolbar 中menu图标不显示问题
    android.support.design.widget.AppBarLayout 在android5.0+底部显示空白条问题
    浅析jQuery框架与构造对象
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/15136442.html
Copyright © 2011-2022 走看看