HOject与Bitmap类型相互转换

/// <summary>
        /// 快速彩色Hobject转bitmap
        /// </summary>
        /// <param name="hObject"></param>
        /// <returns></returns>
        public  Bitmap FastHobject2Bitmap24(HObject hObject)
        {
            //获取图像尺寸
            HTuple width0 = new HTuple();
            HTuple height0 = new HTuple();
            HTuple Pointer = new HTuple();
            HTuple type = new HTuple();
            HTuple width = new HTuple();
            HTuple height = new HTuple();
            HObject InterImage = new HObject();
            HOperatorSet.GetImageSize(hObject, out width0, out height0);
            //创建交错格式图像
            HOperatorSet.InterleaveChannels(hObject, out InterImage, "rgb", 4 * width0, 0);
            //获取交错格式图像指针
            HOperatorSet.GetImagePointer1(InterImage, out Pointer, out type, out width, out height);
            IntPtr ptr = Pointer;
            //构建新Bitmap图像
            Bitmap bitmap = new Bitmap(width / 4, height, width, PixelFormat.Format24bppRgb, ptr);
            InterImage?.Dispose();
            return bitmap;
        }
/// <summary>
        /// 常规彩色Hobject转bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <param name="res24"></param>
        public void CommonHObject2Bpp24_(HObject image, out Bitmap res24)
        {
                HTuple hred, hgreen, hblue, type, width, height;
                
                HOperatorSet.GetImagePointer3(image, out hred, out hgreen, out hblue, out type, out width, out height);
                
                Bitmap res32 = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                
                Rectangle rect = new Rectangle(0, 0, width, height);
                BitmapData bitmapData = res32.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                unsafe
                {
                    byte* bptr = (byte*)bitmapData.Scan0;
                    byte* r = ((byte*)hred.I);
                    byte* g = ((byte*)hgreen.I);
                    byte* b = ((byte*)hblue.I);
                
                    int lengh = width * height;
                    for (int i = 0; i < lengh; i++)
                    {
                        bptr[i * 4] = (b)[i];
                        bptr[i * 4 + 1] = (g)[i];
                        bptr[i * 4 + 2] = (r)[i];
                        bptr[i * 4 + 3] = 255;
                    }
                }
                res32.UnlockBits(bitmapData);                 
                //32位Bitmap转24位
                res24 = new Bitmap(res32.Width, res32.Height,PixelFormat.Format24bppRgb);
                Graphics graphics = Graphics.FromImage(res24);
                graphics.DrawImage(res32, new Rectangle(0, 0, res32.Width,res32.Height)); 
                res32.Dispose();
        }
        /// <summary>
        /// 灰度图hobject图像转bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <param name="res"></param>
        public void HObject2Bitmap8(HObject image, out Bitmap res)
        {
            HTuple hpoint, type, width, height;
            const int Alpha = 255;
            HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
            res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            ColorPalette pal = res.Palette;
            for (int i = 0; i <= 255; i++)
            { pal.Entries[i] = Color.FromArgb(Alpha, i, i, i); }

            res.Palette = pal; Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
            int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
            IntPtr ptr1 = bitmapData.Scan0;
            IntPtr ptr2 = hpoint;
            if (width%4==0)
            {
                int bytes = width * height;
                byte[] rgbvalues = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(ptr2, rgbvalues, 0, bytes);
                System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr1, bytes);
            }
            else
            {
                for (int i = 0; i < height - 1; i++)
                {
                    ptr2 += width.I;
                    CopyMemory(ptr1, ptr2, width * PixelSize);
                    ptr1 += bitmapData.Stride;
                }


            }

            res.UnlockBits(bitmapData);
        }
        [DllImport("kernel32.dll")]
        public extern static long CopyMemory(IntPtr dest, IntPtr source, int size);
        /// <summary>
        /// 灰度图hobject图像转bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <param name="res"></param>
        public void HObject2Bitmap8(HObject image, out Bitmap res)
        {
            HTuple hpoint, type, width, height;
            const int Alpha = 255;
            HOperatorSet.GetImagePointer1(image, out hpoint, out type, out width, out height);
            res = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            ColorPalette pal = res.Palette;
            for (int i = 0; i <= 255; i++)
            { pal.Entries[i] = Color.FromArgb(Alpha, i, i, i); }

            res.Palette = pal; Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bitmapData = res.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
            int PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
            IntPtr ptr1 = bitmapData.Scan0;
            IntPtr ptr2 = hpoint;
            if (width%4==0)
            {
                int bytes = width * height;
                byte[] rgbvalues = new byte[bytes];
                System.Runtime.InteropServices.Marshal.Copy(ptr2, rgbvalues, 0, bytes);
                System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr1, bytes);
            }
            else
            {
                for (int i = 0; i < height - 1; i++)
                {
                    ptr2 += width.I;
                    CopyMemory(ptr1, ptr2, width * PixelSize);
                    ptr1 += bitmapData.Stride;
                }


            }

            res.UnlockBits(bitmapData);
        }
/// <summary>
        /// 灰度bitmap转hobject
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="image"></param>
        public  void Bitmap2HObjectBpp8(Bitmap bmp, out HObject image)
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

                HOperatorSet.GenImage1(out image, "byte", bmp.Width, bmp.Height, srcBmpData.Scan0);
                bmp.UnlockBits(srcBmpData);
            }
            catch (Exception ex)
            {
                image = null;
            }
        }
 /// <summary>
        /// 彩色bitmap转bitmap
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="image"></param>
        public  void Bitmap2HObjectBpp24(Bitmap bmp, out HObject image) 
        {
            try
            {
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

                BitmapData srcBmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                HOperatorSet.GenImageInterleaved(out image, srcBmpData.Scan0, "bgr", bmp.Width, bmp.Height, 0, "byte", 0, 0, 0, 0, -1, 0);
                bmp.UnlockBits(srcBmpData);

            }
            catch (Exception ex)
            {
                image = null;
            }
        }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值