示例#1
0
        public static Bitmap toBmp(this ImageRgb img)
        {
            int    w   = img.width();
            int    h   = img.height();
            Bitmap bmp = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            try
            {
                unsafe
                {
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h),
                                                      System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                                      System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                    byte[] rawData = new byte[bmpData.Stride * bmpData.Height];
                    Marshal.Copy(img.getRawImage(), rawData, 0, rawData.Length);

                    Marshal.Copy(rawData, 0, bmpData.Scan0, rawData.Length);

                    bmp.UnlockBits(bmpData);
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Exception while accessing image: " + e);
            }

            return(bmp);
        }
示例#2
0
        public static ImageRgb toImgRgb(this Bitmap bmp)
        {
            ImageRgb img = new ImageRgb();

            img.resize(bmp.Width, bmp.Height);
            try
            {
                unsafe
                {
                    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, img.width(), img.height()),
                                                      System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                                      System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                    byte[] rawData = new byte[bmpData.Stride * bmpData.Height];
                    Marshal.Copy(bmpData.Scan0, rawData, 0, rawData.Length);

                    Marshal.Copy(rawData, 0, img.getRawImage(), rawData.Length);


                    bmp.UnlockBits(bmpData);
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Exception while accessing image: " + e);
            }

            return(img);
        }