热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GDI+问题:Image对象转换Bitmap

问下Image指针可以强转为Bitmap指针使用么?如果不行,怎样将Image转换为Bitmap对象?
问下Image指针可以强转为Bitmap指针使用么?如果不行, 怎样将Image转换为Bitmap对象?

6 个解决方案

#1


http://www.cnblogs.com/peasana/archive/2012/02/13/2349165.html
    /// 
        /// 将图片Image转换成Byte[]
        /// 

        /// image对象
        /// 后缀名
        /// 
        public static byte[] ImageToBytes(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)
        {
 
            if (Image == null) { return null; }
 
            byte[] data = null;
 
            using (MemoryStream ms= new MemoryStream())
            {
 
                 using (Bitmap Bitmap = new Bitmap(Image))
                {
 
                    Bitmap.Save(ms, imageFormat);
 
                    ms.Position = 0;
 
                    data = new byte[ms.Length];
 
                    ms.Read(data, 0, Convert.ToInt32(ms.Length));
 
                    ms.Flush();
 
                }
 
            }
 
            return data;
 
        }
 
 
 
 
 
            /// 
            /// byte[]转换成Image
            /// 

            /// 二进制图片流
            /// Image
            public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
            {
                if (byteArrayIn == null)
                    return null;
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
                {
                    System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
                    ms.Flush();
                    return returnImage;
                }
            }
 
 
 
    //Image转换Bitmap
 
   1. Bitmap img = new Bitmap(imgSelect.Image);
 
   2. Bitmap bmp = (Bitmap)pictureBox1.Image;
 
 
 
//Bitmap转换成Image
 
using System.IO;
 
private static System.Windows.Controls.Image Bitmap2Image(System.Drawing.Bitmap Bi)
        {           
            MemoryStream ms = new MemoryStream();
            Bi.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            BitmapImage bImage = new BitmapImage();
            bImage.BeginInit();
            bImage.StreamSource = new MemoryStream(ms.ToArray());
            bImage.EndInit();
            ms.Dispose();
            Bi.Dispose();
            System.Windows.Controls.Image i = new System.Windows.Controls.Image();
            i.Source = bImage;
            return i ;
        }
 
 
 
//byte[] 转换 Bitmap
 public static Bitmap BytesToBitmap(byte[] Bytes) 
        { 
            MemoryStream stream = null; 
            try 
            { 
                stream = new MemoryStream(Bytes); 
                return new Bitmap((Image)new Bitmap(stream)); 
            } 
            catch (ArgumentNullException ex) 
            { 
                throw ex; 
            } 
            catch (ArgumentException ex) 
            { 
                throw ex; 
            } 
            finally 
            { 
                stream.Close(); 
            } 
        }  
 
//Bitmap转byte[]  
        public static byte[] BitmapToBytes(Bitmap Bitmap) 
        { 
            MemoryStream ms = null; 
            try 
            { 
                ms = new MemoryStream(); 
                Bitmap.Save(ms, Bitmap.RawFormat); 
                byte[] byteImage = new Byte[ms.Length]; 
                byteImage = ms.ToArray(); 
                return byteImage; 
            } 
            catch (ArgumentNullException ex) 
            { 
                throw ex; 
            } 
            finally 
            { 
                ms.Close(); 
            } 
        } 
    } 

#2


1、有关IPicture加载图片后直接Render到内存DC的问题(HBITMAP 转换 IPicture)

Picture的方法get_Handle可以直接得到图片的句柄 

IPicture *pIPicture; 

HBITMAP hBitmap; 

... 

pIPicture->get_Handle((OLE_HANDLE *)&hBitmap);

2、CBitmap 转换 HBITMAP 

CBitmap m_bitMap; 

HBITMAP m_hBitMap; 

m_bitMap.LoadBitmap(IDB_BITMAP); 

m_hBitMap=(HBITMAP)m_bitMap.GetSafeHandle();

3、HBITMAP 转换 CBitmap 

    CBitmap cbMMyBitmap;//用来显示动画的位图

    HBITMAP hMMyHBitmap;//用来显示动画的位图的句柄

    BITMAP bMMyBitmapInfo;//位图信息

    cbMMyBitmap.Attach(hMMyHBitmap); 

    cbMMyBitmap.GetBitmap(&bMMyBitmapInfo);

4、DrawDibDraw 和AlphaBlend

一:这两个函数都是绘制 DIB 的。 

我在使用过程中发现这两个函数对于 32 位的位图,Alpha通道不能正确显示。就是说,该透明的地方不透明。 

说明一下,位图没有问题的,用看图软件或PS之类的都可以正确显示。 

我想问一下,是我的原因还是这两个函数本来就不支持32位位图? 

ps.显示32位位图可以用AlphaBlend实现的,但是这个函数是绘制场景里的 DDB 的。由于我想显示的是 DIB,不想转来转去的,所以这个函数先不考虑。

二:StretchDIBits 或 DrawDibDraw 就没法显示带透明属性的图片 

要显示带透明属性的图片只能用别的方法 例如 GDI+   

5、怎样 样把一个BITMAP结构赋给CBitmap对象

回答(1)

CBitmap::GetBitmap

回答人:软界网友 

回答(2)

每个成员变量都赋值。

回答人:软界网友 

回答(3)

CBitmap::GetBitmap是把CBitmap的信息赋给BITMAP结构 

我问的是BITMAP赋给CBitmap

回答人:软界网友 

回答(4)

创建CBitmap对象时,采用BITMAP指定信息

回答人:软界网友 

回答(5)

是用构造函数吗? 

CBitmap::CBitmap(BITMAP bitmap)好像没有这个构造函数

回答人:软界网友 

回答(6)

CBitmap::FromHandle(HBITMAP hBitmap )

回答(7)

HBITMAP CreateBitmapIndirect( 

CONST BITMAP *lpbm // bitmap data 

); 

6、用GDI实现位图透明显示

下面是实现中几个关键的地方:

//////////////////////////////////////////////////////////////////////////

////把位图bmpFile的特定矩形区域rtWant上的所有像素的数据读取到缓冲区pBuf中

int CChildView::GetPixFromBmp(CString fileName,CRect rtWant,BYTE *pBuf)

{

BITMAPINFOHEADER headInfo;

ZeroMemory(&headInfo,sizeof(headInfo));

int imageHeight;

int imageWidth;

CFile file;

file.Open(fileName, CFile::modeRead);

file.Seek(14, CFile::begin);

file.Read(&headInfo, 40); ///-- 从文件中读去位图的BITMAPINFOHEADER信息

imageWidth = headInfo.biWidth;

imageHeight = headInfo.biHeight;

if(headInfo.biBitCount!=32)

{

   MessageBox(fileName+"不是32位位图");

}

for(int i=0;i< P> 

{

   file.Seek(54+(imageHeight-     rtWant.bottom+i)*imageWidth*headInfo.biBitCount/8+rtWant.left*headInfo.biBitCount/8,CFile::begin);

   file.Read(pBuf+i*rtWant.Width()*headInfo.biBitCount/8,rtWant.Width()*headInfo.biBitCount/8); 

}

file.Close();

return 0;

}

//////////////////////////////////////////////////////////////////////////

//// pDesBuf:目的表面缓冲区,W,H--目的表面的宽和高

//// pSrcBuf: 源表面缓冲区

//// pRetBuf:结果保存到这个缓冲区中,与pDesBuf兼容

//// rtBlend:源表面缓冲区与目的表面缓冲区进行alpha混合的矩形区域

//// alpha : 需要透明的百分比,0---完全透明 ,1 -- 完全不透明

void CChildView::MyBlend(BYTE* pDesBuf,BYTE* pSrcBuf,BYTE* pRetBuf,CRect rtBlend,int W,int H,float alpha)

{

memcpy(pRetBuf,pDesBuf,W*H*4);

for(int i=0;i< P> 

{

   for(int j=0;j< P> 

   {

    long posDesPix = ((H-rtBlend.bottom+i)*W+rtBlend.left+j)*4;

    long posSrcPix = (i*rtBlend.Width()+j)*4;

    pRetBuf[posDesPix+0] = BYTE(pDesBuf[posDesPix+0]*(1-alpha) + pSrcBuf[posSrcPix+0]*(alpha));

    pRetBuf[posDesPix+1] = BYTE(pDesBuf[posDesPix+1]*(1-alpha) + pSrcBuf[posSrcPix+1]*(alpha));

    pRetBuf[posDesPix+2] = BYTE(pDesBuf[posDesPix+2]*(1-alpha) + pSrcBuf[posSrcPix+2]*(alpha));

    pRetBuf[posDesPix+3] = BYTE(pDesBuf[posDesPix+3]*(1-alpha) + pSrcBuf[posSrcPix+3]*(alpha));

   }

}

}

{

///-- 设置位图的BITMAPINFOHEADER信息

ZeroMemory(&m_headInfo,sizeof(m_headInfo));

m_headInfo.biSize = sizeof(m_headInfo);

m_headInfo.biPlanes = 1;

m_headInfo.biBitCount = 32; // 24

m_headInfo.biCompression = BI_RGB;

m_headInfo.biWidth = m_rtShow.Width();

m_headInfo.biHeight = m_rtShow.Height();

m_headInfo.biSizeImage = m_nLen;

}

void CChildView::OnPaint() 

{

CPaintDC dc(this); // device context for painting

StretchDIBits(m_MemDC.GetSafeHdc(),0,0,m_rtShow.Width(),m_rtShow.Height(),

   0,0,m_rtShow.Width(),m_rtShow.Height(),(void*)m_pCurrBuf,(BITMAPINFO*)&m_headInfo,NULL,SRCCOPY);

dc.BitBlt(m_rtShow.left,m_rtShow.top,m_rtShow.Width(),m_rtShow.Height(),

   &m_MemDC,0,0,SRCCOPY);

}

 

http://hi.baidu.com/ustc_/blog/item/803f084418895d44510ffee3.html

 

#3


....楼上的兄弟, 请不要不清不楚乱复制粘贴, 这里是VC/MFC, 不是C#, 上面的那么一大坨东西也没说到点上。

#4


class Bitmap : public Image

你可以试试 。。

#5


Bitmap是Image的子类

#6


想问个简单的问题哈,在将像png类的 image convert 成 bmp的时候,image的质量,比如分辨率啊,颜色之类的信息会不会出现失真的情况哇?

推荐阅读
author-avatar
一支妙笔生花来
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有