c#与C++dll互传图片

直接上代码,

public static BitmapInfo GetImagePixel(Bitmap Source)
        {
            byte[] result;
            int step;
            int iWidth = Source.Width;
            int iHeight = Source.Height;
            //
            if (iWidth %4!=0)
            {
                iWidth = iWidth - (iWidth % 4);
            }
            Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
            System.Drawing.Imaging.BitmapData bmpData = Source.LockBits(rect,
                System.Drawing.Imaging.ImageLockMode.ReadWrite, Source.PixelFormat);
            IntPtr iPtr = bmpData.Scan0;
            //int iBytes = iWidth * iHeight * 3;
            step = bmpData.Stride/iWidth;
            int iBytes = iWidth * iHeight * step;
            byte[] PixelValues = new byte[iBytes];
            System.Runtime.InteropServices.Marshal.Copy(iPtr, PixelValues, 0, iBytes);
            Source.UnlockBits(bmpData);
            result = PixelValues;
            BitmapInfo bi = new BitmapInfo { Result = result, Channels = step, width =iWidth, height = iHeight};
            return bi;
        }
        public static Bitmap SetImagePixel(ref Bitmap Source,byte[] imgData,int iWidth, int iHeight, int step)
        {
            byte[] result;
            //
            if (iWidth % 4 != 0)
            {
                iWidth = iWidth - (iWidth % 4);
            }
            Rectangle rect = new Rectangle(0, 0, iWidth, iHeight);
            System.Drawing.Imaging.BitmapData bmpData = Source.LockBits(rect,
                System.Drawing.Imaging.ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            IntPtr iPtr = bmpData.Scan0;
            //int iBytes = iWidth * iHeight * 3;
            int iBytes = iWidth * iHeight * step;
            //byte[] PixelValues = new byte[iBytes];
            //bmpData.Scan0 = System.Runtime.InteropServices.Marshal.AllocHGlobal(iBytes);  //这句代码必须删除,否则会出现内存问题
            System.Runtime.InteropServices.Marshal.Copy(imgData, 0, bmpData.Scan0, iBytes);

            Source.UnlockBits(bmpData);
            result = imgData;
           // BitmapInfo bi = new BitmapInfo { Result = result, Channels = step, width = iWidth, height = iHeight };
            return Source;
        }
BitmapInfo 类定义
public class BitmapInfo
    {
        public byte[] Result { get; set; }
        public int Channels { get; set; }
        public int width { get; set; }
        public int height { get; set; }
    }

看到网上所有方法都只有c#传图片给c++, 却没有从c++获取图片后,怎么转回来,因此研究了代码后,写了

SetImagePixel()方法,此方法经测试有效,调用实例如下:
          Bitmap b = new Bitmap(bitmap.width, bitmap.height);
            GetImagePixel(ref b,bitmap.Result, bitmap.width, bitmap.height, bitmap.Channels);
            b.Save("D:\\hhh.jpg");        

 

 

原文链接: https://www.cnblogs.com/Betty-IT/p/12929029.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    c#与C++dll互传图片

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/349878

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午5:58
下一篇 2023年3月2日 上午5:58

相关推荐