游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2234|回复: 2

论如何从数组中还原图片对象

[复制链接]

2

主题

45

帖子

45

积分

注册会员

Rank: 2

积分
45
发表于 2006-5-13 10:46:00 | 显示全部楼层 |阅读模式
这或许不是最好的方法,但绝对可行,注意以下是我的图片打包程序中截取片断稍加修改的,领会精神就可以,复制粘贴不一定能运行。

Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
Private Declare Function OleLoadPicture Lib "olepro32" (pStream As Any, ByVal lSize As Long, ByVal fRunmode As Long, riid As Any, ppvObj As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Any, pclsid As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal HDC As Long, ByVal hObject As Long) As Long
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal HDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal HDC As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Dim memDC As Long
Dim lpMem  As Long
Dim IID_IPicture(15)
Dim istm As stdole.IUnknown
Dim myIPicture As IPicture
Dim myStdPicture As StdPicture
Dim TempDC As Long, hBmp As Long, w As Long, h As Long, bmpInfo As Bitmap
Dim b() As Byte

'1. 把图片读入数组b()中,假设这步骤已经完成。

'2. 从数组b()中建立图片对象,并存放在一个内存DC中
   memDC = GlobalAlloc(&H2, Ubound(b))
   If memDC <> 0 Then
    lpMem = GlobalLock(memDC)
    If lpMem <> 0 Then
        MoveMemory ByVal lpMem, b(0), Ubound(b)
        Call GlobalUnlock(memDC)
        If CreateStreamOnHGlobal(memDC, 1, istm) = 0 Then
            If CLSIDFromString(StrPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}"), IID_IPicture(0)) = 0 Then
              Call OleLoadPicture(ByVal ObjPtr(istm), ByteCount, 0, IID_IPicture(0), myIPicture)
            End If
        End If
    End If
   End If  
   Set myStdPicture = myIPicture
   GetObject myStdPicture.Handle, Len(bmpInfo), bmpInfo
    w = bmpInfo.bmWidth
    h = bmpInfo.bmHeight
    TempDC = CreateCompatibleDC(0)
    hBmp = SelectObject(TempDC, myStdPicture.Handle)
   'TempDC 就是存有图片的内存DC的指针

'3. 如何显示图片: BitBlt picture1.hDC, 0, 0, w, h, TempDC, 0, 0, SRCCOPY

'4. 其实它最大的用处就是可以实现图片资源打包,并在内存中读取,还原,不用生成临时图片文件。

26

主题

537

帖子

537

积分

高级会员

Rank: 4

积分
537
发表于 2006-5-15 09:14:00 | 显示全部楼层

Re:论如何从数组中还原图片对象

CLSIDFromString(StrPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}")是什么签名?

10

主题

62

帖子

68

积分

注册会员

Rank: 2

积分
68
发表于 2006-5-22 17:31:00 | 显示全部楼层

Re: Re:论如何从数组中还原图片对象

系统崩溃: Re:论如何从数组中还原图片对象

CLSIDFromString(StrPtr("{7BF80980-BF32-101A-8BBB-00AA00300CAB}")是什么签名?



是IPicture接口,VB的Picture对象(或IPictureDisp。不是StdPicture!!!)


stdole2.tlb

  1.     [
  2.       odl,
  3.       uuid(7BF80980-BF32-101A-8BBB-00AA00300CAB),
  4.       helpstring("Picture Object"),
  5.       hidden
  6.     ]
  7.     interface IPicture : IUnknown {
  8.         [propget]
  9.         HRESULT _stdcall Handle([out, retval] OLE_HANDLE* phandle);
  10.         [propget]
  11.         HRESULT _stdcall hPal([out, retval] OLE_HANDLE* phpal);
  12.         [propget]
  13.         HRESULT _stdcall Type([out, retval] short* ptype);
  14.         [propget]
  15.         HRESULT _stdcall Width([out, retval] OLE_XSIZE_HIMETRIC* pwidth);
  16.         [propget]
  17.         HRESULT _stdcall Height([out, retval] OLE_YSIZE_HIMETRIC* pheight);
  18.         HRESULT _stdcall Render(
  19.                         [in] int hdc,
  20.                         [in] long x,
  21.                         [in] long y,
  22.                         [in] long cx,
  23.                         [in] long cy,
  24.                         [in] OLE_XPOS_HIMETRIC xSrc,
  25.                         [in] OLE_YPOS_HIMETRIC ySrc,
  26.                         [in] OLE_XSIZE_HIMETRIC cxSrc,
  27.                         [in] OLE_YSIZE_HIMETRIC cySrc,
  28.                         [in] void* prcWBounds);
  29.         [propput]
  30.         HRESULT _stdcall hPal([in] OLE_HANDLE phpal);
  31.         [propget]
  32.         HRESULT _stdcall CurDC([out, retval] int* phdcOut);
  33.         HRESULT _stdcall SelectPicture(
  34.                         [in] int hdcIn,
  35.                         [out] int* phdcOut,
  36.                         [out] OLE_HANDLE* phbmpOut);
  37.         [propget]
  38.         HRESULT _stdcall KeepOriginalFormat([out, retval] VARIANT_BOOL* pfkeep);
  39.         [propput]
  40.         HRESULT _stdcall KeepOriginalFormat([in] VARIANT_BOOL pfkeep);
  41.         HRESULT _stdcall PictureChanged();
  42.         HRESULT _stdcall SaveAsFile(
  43.                         [in] void* pstm,
  44.                         [in] VARIANT_BOOL fSaveMemCopy,
  45.                         [out] long* pcbSize);
  46.         [propget]
  47.         HRESULT _stdcall Attributes([out, retval] long* pdwAttr);
  48.         HRESULT _stdcall SetHdc([in] OLE_HANDLE hdc);
  49.     };

  50.     [
  51.       uuid(7BF80981-BF32-101A-8BBB-00AA00300CAB)
  52.     ]
  53.     dispinterface Picture {
  54.         properties:
  55.             [id(00000000), readonly            
  56. ]
  57.             OLE_HANDLE Handle;
  58.             [id(0x00000002)            
  59. ]
  60.             OLE_HANDLE hPal;
  61.             [id(0x00000003), readonly            
  62. ]
  63.             short Type;
  64.             [id(0x00000004), readonly            
  65. ]
  66.             OLE_XSIZE_HIMETRIC Width;
  67.             [id(0x00000005), readonly            
  68. ]
  69.             OLE_YSIZE_HIMETRIC Height;
  70.         methods:
  71.             [id(0x00000006)]
  72.             void Render(
  73.                             int hdc,
  74.                             long x,
  75.                             long y,
  76.                             long cx,
  77.                             long cy,
  78.                             OLE_XPOS_HIMETRIC xSrc,
  79.                             OLE_YPOS_HIMETRIC ySrc,
  80.                             OLE_XSIZE_HIMETRIC cxSrc,
  81.                             OLE_YSIZE_HIMETRIC cySrc,
  82.                             void* prcWBounds);
  83.     };

  84.     typedef [public]
  85.     Picture IPictureDisp;

  86.     [
  87.       uuid(0BE35204-8F91-11CE-9DE3-00AA004BB851)
  88.     ]
  89.     coclass StdPicture {
  90.         [default] dispinterface Picture;
  91.         interface IPicture;
  92.     };
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 14:07

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表