Advertisement
Guest User

Class cDIBSection

a guest
Jun 27th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 18.34 KB | None | 0 0
  1. Option Compare Database
  2. Option Explicit
  3.  
  4.  
  5. Private Type RECT
  6.     Left As Long
  7.     top As Long
  8.     right As Long
  9.     Bottom As Long
  10. End Type
  11.  
  12. Private Type SIZEL
  13.     cx As Long
  14.     cy As Long
  15. End Type
  16.  
  17. Private Type ENHMETAHEADER
  18.         iType As Long
  19.         nSize As Long
  20.         rclBounds As RECT
  21.         rclFrame As RECT
  22.         dSignature As Long
  23.         nVersion As Long
  24.         nBytes As Long
  25.         nRecords As Long
  26.         nHandles As Integer
  27.         sReserved As Integer
  28.         nDescription As Long
  29.         offDescription As Long
  30.         nPalEntries As Long
  31.         szlDevice As SIZEL
  32.         szlMillimeters As SIZEL
  33. End Type
  34.  
  35.  
  36. Private Type RGBQUAD
  37.   rgbBlue As Byte
  38.   rgbGreen As Byte
  39.   rgbRed As Byte
  40.   rgblReterved As Byte
  41. End Type
  42.  
  43.  
  44. 'Private Enum ERGBCompression
  45. Private Const BI_RGB = 0&
  46.   Private Const BI_RLE4 = 2&
  47.   Private Const BI_RLE8 = 1&
  48.   Private Const DIB_RGB_COLORS = 0 '  color table in RGBs
  49. 'End Enum
  50.  
  51.  
  52. Private Type BITMAPINFOHEADER '40 bytes
  53.  biSize As Long
  54.   biWidth As Long
  55.   biHeight As Long
  56.   biPlanes As Integer
  57.   biBitCount As Integer
  58.   biCompression As Long 'ERGBCompression
  59.  biSizeImage As Long
  60.   biXPelsPerMeter As Long
  61.   biYPelsPerMeter As Long
  62.   biClrUsed As Long
  63.   biClrImportant As Long
  64. End Type
  65.  
  66.  
  67. Private Type BITMAPINFO
  68.   bmiHeader As BITMAPINFOHEADER
  69.   bmiColors As RGBQUAD
  70. End Type
  71.  
  72.  
  73. Private Type BITMAP
  74.   bmType As Long
  75.   bmWidth As Long
  76.   bmHeight As Long
  77.   bmWidthBytes As Long
  78.   bmPlanes As Integer
  79.   bmBitsPixel As Integer
  80.   bmBits As Long
  81. End Type
  82.  
  83. Private Type DIBSECTION
  84.     dsBm As BITMAP
  85.     dsBmih As BITMAPINFOHEADER
  86.     dsBitfields(2) As Long
  87.     dshSection As Long
  88.     dsOffset As Long
  89. End Type
  90.  
  91. Private Type METAFILEPICT
  92.  mm As Long
  93.  xExt As Long
  94.  yExt As Long
  95.  hMF As Long
  96. End Type
  97.  
  98. ' From winuser.h
  99. Private Const IMAGE_BITMAP = 0
  100. Private Const IMAGE_ICON = 1
  101. Private Const IMAGE_CURSOR = 2
  102. Private Const IMAGE_ENHMETAFILE = 3
  103.  
  104. Private Const LR_DEFAULTCOLOR = &H0
  105. Private Const LR_MONOCHROME = &H1
  106. Private Const LR_COLOR = &H2
  107. Private Const LR_COPYRETURNORG = &H4
  108. Private Const LR_COPYDELETEORG = &H8
  109. Private Const LR_LOADFROMFILE = &H10
  110. Private Const LR_LOADTRANSPARENT = &H20
  111. Private Const LR_DEFAULTSIZE = &H40
  112. Private Const LR_VGACOLOR = &H80
  113. Private Const LR_LOADMAP3DCOLORS = &H1000
  114. Private Const LR_CREATEDIBSECTION = &H2000
  115. Private Const LR_COPYFROMRESOURCE = &H4000
  116. Private Const LR_SHARED = &H8000
  117.  
  118. Private Const vbSrcCopy = &HCC0020
  119. Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
  120. Private Const WHITENESS = &HFF0062 ' (DWORD) dest = WHITE
  121. Private Const BLACKNESS = &H42 ' (DWORD) dest = BLACK
  122.  
  123. ' Note - this is not the declare in the API viewer - modify lplpVoid to be
  124. ' Byref so we get the pointer back:
  125. Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
  126. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  127. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  128. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  129. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  130. Private Declare Function GetDIBits Lib "gdi32" (ByVal hdc As Long, ByVal hBmp As Long, ByVal uStartScan As Long, ByVal cScanLines As Long, ByVal lpvBits As Long, ByRef lpbi As BITMAPINFO, ByVal uUsage As Long) As Long
  131. Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInstance As Long, ByVal Name As Long, ByVal uType As Long, ByVal cxDesired As Long, ByVal cyDesired As Long, ByVal fuLoad As Long) As Long
  132. Private Declare Function apiGetObject Lib "gdi32" Alias "GetObjectA" _
  133. (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  134. Private Declare Sub apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  135. (Destination As Any, Source As Any, ByVal Length As Long)
  136.  
  137. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  138.  
  139. Private Declare Function apiGetDeviceCaps Lib "gdi32" _
  140. Alias "GetDeviceCaps" (ByVal hdc As Long, ByVal nIndex As Long) As Long
  141.  
  142. ' Create an Information Context
  143. Private Declare Function apiCreateIC Lib "gdi32" Alias "CreateICA" _
  144. (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
  145. ByVal lpOutput As String, lpInitData As Any) As Long
  146.  
  147. Private Declare Function apiPlayEnhMetaFile Lib "gdi32" Alias "PlayEnhMetaFile" (ByVal hdc As Long, ByVal hEMF As Long, lpRect As RECT) As Long
  148.  
  149. Private Declare Function SetWinMetaFileBits Lib "gdi32" _
  150. (ByVal cbBuffer As Long, lpbBuffer As Byte, _
  151. ByVal hDCRef As Long, lpmfp As METAFILEPICT) As Long
  152.  
  153.  
  154. Private Declare Function apiDeleteEnhMetaFile Lib "gdi32" Alias "DeleteEnhMetaFile" _
  155. (ByVal hEMF As Long) As Long
  156.  
  157. Private Declare Function apiCloseEnhMetaFile Lib "gdi32" Alias "CloseEnhMetaFile" _
  158. (ByVal hdc As Long) As Long
  159.  
  160. Private Declare Function GetEnhMetaFileHeader Lib "gdi32" _
  161. (ByVal hEMF As Long, ByVal cbBuffer As Long, lpemh As ENHMETAHEADER) As Long
  162.  
  163. Private Declare Function apiDeleteDC Lib "gdi32" _
  164.   Alias "DeleteDC" (ByVal hdc As Long) As Long
  165.  
  166. Private Declare Function apiCreateSolidBrush Lib "gdi32" Alias "CreateSolidBrush" _
  167.     (ByVal crColor As Long) As Long
  168.  
  169. Private Declare Function apiFillRect Lib "user32" Alias "FillRect" _
  170. (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
  171.  
  172.  
  173. ' Predefined Clipboard Formats
  174. Private Const CF_TEXT = 1
  175. Private Const CF_BITMAP = 2
  176. Private Const CF_METAFILEPICT = 3
  177. Private Const CF_SYLK = 4
  178. Private Const CF_DIF = 5
  179. Private Const CF_TIFF = 6
  180. Private Const CF_OEMTEXT = 7
  181. Private Const CF_DIB = 8
  182. Private Const CF_PALETTE = 9
  183. Private Const CF_PENDATA = 10
  184. Private Const CF_RIFF = 11
  185. Private Const CF_WAVE = 12
  186. Private Const CF_UNICODETEXT = 13
  187. Private Const CF_ENHMETAFILE = 14
  188.  
  189. '  Device Parameters for GetDeviceCaps()
  190. Private Const LOGPIXELSX = 88        '  Logical pixels/inch in X
  191. Private Const LOGPIXELSY = 90        '  Logical pixels/inch in Y
  192.  
  193. ' Handle to the current DIBSection:
  194. Private m_hDib As Long
  195. ' Handle to the old bitmap in the DC, for clear up:
  196. Private m_hBmpOld As Long
  197. ' Handle to the Device context holding the DIBSection:
  198. Private m_hDC As Long
  199. ' Address of memory pointing to the DIBSection's bits:
  200. Private m_lPtr As Long
  201. ' Type containing the Bitmap information:
  202. Private m_bmi As BITMAPINFO
  203. ' Holds current JPEG's FileName
  204. Private m_CurrentJpegFileName As String
  205. ' Array to hold original compressed Jpeg
  206. ' to be used for BLOB storage in Table
  207. Private bArray() As Byte
  208.  
  209. ' Temp var
  210. Dim lngRet As Long
  211.  
  212.  
  213.  
  214. Public Function CreateDIB( _
  215.   ByVal lhdc As Long, _
  216.   ByVal lWidth As Long, _
  217.   ByVal lHeight As Long, _
  218.   ByVal lChannels As Long, _
  219.   ByRef hDib As Long _
  220.   ) As Boolean
  221.    
  222.   With m_bmi.bmiHeader
  223.     .biSize = Len(m_bmi.bmiHeader)
  224.     .biWidth = lWidth
  225.     .biHeight = lHeight
  226.     .biPlanes = 1
  227.     If lChannels = 3 Then
  228.       .biBitCount = 24
  229.     Else
  230.       .biBitCount = 32
  231.     End If
  232.     .biCompression = BI_RGB
  233.     .biSizeImage = BytesPerScanLine * .biHeight
  234.   End With
  235.  
  236.   'The m_lPtr is passed in byref.. so that it returns the the pointer to the bitmapinfo bits
  237.  'the m_lptr is then stored as a reference to the uncompressed image data
  238.  'the m_lptr is filled with image data when the ijlread method is invoked.
  239.  hDib = CreateDIBSection(lhdc, m_bmi, DIB_RGB_COLORS, m_lPtr, 0, 0)
  240.  
  241.   CreateDIB = (hDib <> 0)
  242.  
  243. End Function
  244.  
  245.  
  246. Public Function Create(ByVal lWidth As Long, ByVal lHeight As Long, Optional ByVal lChannels As Long = 3) As Boolean
  247.  
  248.   CleanUp
  249.  
  250.   m_hDC = CreateCompatibleDC(0)
  251.  
  252.   If (m_hDC <> 0) Then
  253.     If (CreateDIB(m_hDC, lWidth, lHeight, lChannels, m_hDib)) Then
  254.       m_hBmpOld = SelectObject(m_hDC, m_hDib)
  255.       Create = True
  256.     Else
  257.       Call DeleteObject(m_hDC)
  258.       m_hDC = 0
  259.     End If
  260.   End If
  261.  
  262. End Function
  263.  
  264.  
  265. Public Function Load(ByVal Name As String) As Boolean
  266.   Dim hBmp As Long
  267.   Dim pName As Long
  268.   Dim aName As String
  269.  
  270.   Load = False
  271.  
  272.   CleanUp
  273.  
  274.   m_hDC = CreateCompatibleDC(0)
  275.   If m_hDC = 0 Then
  276.     Exit Function
  277.   End If
  278.  
  279.   aName = StrConv(Name, vbFromUnicode)
  280.   pName = StrPtr(aName)
  281.  
  282.   hBmp = LoadImage(0, pName, IMAGE_BITMAP, 0, 0, (LR_CREATEDIBSECTION Or LR_LOADFROMFILE))
  283.   If hBmp = 0 Then
  284.     Call DeleteObject(m_hDC)
  285.     m_hDC = 0
  286.     MsgBox "Can't load BMP image"
  287.     Exit Function
  288.   End If
  289.  
  290.   m_bmi.bmiHeader.biSize = Len(m_bmi.bmiHeader)
  291.  
  292.   ' get image sizes
  293.  Call GetDIBits(m_hDC, hBmp, 0, 0, 0, m_bmi, DIB_RGB_COLORS)
  294.  
  295.   ' make 24 bpp dib section
  296.  m_bmi.bmiHeader.biBitCount = 24
  297.   m_bmi.bmiHeader.biCompression = BI_RGB
  298.   m_bmi.bmiHeader.biClrUsed = 0
  299.   m_bmi.bmiHeader.biClrImportant = 0
  300.  
  301.   m_hDib = CreateDIBSection(m_hDC, m_bmi, DIB_RGB_COLORS, m_lPtr, 0, 0)
  302.   If m_hDib = 0 Then
  303.     Call DeleteObject(hBmp)
  304.     Call DeleteObject(m_hDC)
  305.     m_hDC = 0
  306.     Exit Function
  307.   End If
  308.  
  309.   m_hBmpOld = SelectObject(m_hDC, m_hDib)
  310.  
  311.   m_bmi.bmiHeader.biSize = Len(m_bmi.bmiHeader)
  312.  
  313.   ' get image data in 24 bpp format (convert if need)
  314.  Call GetDIBits(m_hDC, hBmp, 0, m_bmi.bmiHeader.biHeight, m_lPtr, m_bmi, DIB_RGB_COLORS)
  315.  
  316.   Call DeleteObject(hBmp)
  317.  
  318.   Load = True
  319.  
  320. End Function
  321.  
  322.  
  323. Public Property Get BytesPerScanLine() As Long
  324.   ' Scans must align on dword boundaries:
  325.  BytesPerScanLine = (m_bmi.bmiHeader.biWidth * (m_bmi.bmiHeader.biBitCount / 8) + 3) And &HFFFFFFFC
  326. End Property
  327.  
  328.  
  329. Public Property Get dib_width() As Long
  330.   dib_width = m_bmi.bmiHeader.biWidth
  331. End Property
  332.  
  333.  
  334. Public Property Get dib_height() As Long
  335.   dib_height = m_bmi.bmiHeader.biHeight
  336. End Property
  337.  
  338.  
  339. Public Property Get dib_channels() As Long
  340.   dib_channels = m_bmi.bmiHeader.biBitCount / 8
  341. End Property
  342.  
  343. Public Property Get CurrentJpegFileName() As String
  344. CurrentJpegFileName = m_CurrentJpegFileName
  345. End Property
  346.  
  347. Public Sub PaintPicture( _
  348.   ByVal lhdc As Long, _
  349.   Optional ByVal lDestLeft As Long = 0, _
  350.   Optional ByVal lDestTop As Long = 0, _
  351.   Optional ByVal lDestWidth As Long = -1, _
  352.   Optional ByVal lDestHeight As Long = -1, _
  353.   Optional ByVal lSrcLeft As Long = 0, _
  354.   Optional ByVal lSrcTop As Long = 0, _
  355.   Optional ByVal eRop As Long) ' = vbSrcCopy)
  356.  
  357.   If (lDestWidth < 0) Then lDestWidth = m_bmi.bmiHeader.biWidth
  358.   If (lDestHeight < 0) Then lDestHeight = m_bmi.bmiHeader.biHeight
  359. Dim lngRet As Long
  360.   lngRet = BitBlt(lhdc, lDestLeft, lDestTop, lDestWidth, lDestHeight, m_hDC, lSrcLeft, lSrcTop, vbSrcCopy)
  361. 'lngRet = BitBlt(lhDC, lDestLeft, lDestTop, 640, 480, m_hDC, lSrcLeft, lSrcTop, vbSrcCopy)
  362.  
  363. End Sub
  364.  
  365. Public Function LoadJpegFileIntoArray() As Boolean
  366.  
  367. On Error GoTo Err_CmdLoad_Click
  368.  
  369. Dim blRet As Boolean
  370.  
  371.  ' jpg_scale = 1
  372.  Dim strfName As String
  373.   strfName = Me.CurrentJpegFileName  ' m_cDib.FileDialog 'c:\test2.jpg"
  374.  ' Read JPEG image
  375.  
  376. Dim lPtr As Long
  377. Dim lSize As Long
  378. Dim iFile As Integer
  379. Dim sFile As String
  380. 'Dim bArray() As Byte
  381.    
  382.    ' Copy the current Jpeg file data directly to the buffer
  383.   iFile = FreeFile
  384.    Open strfName For Binary Access Read Lock Write As #iFile
  385.    lSize = LOF(iFile)
  386.    ReDim bArray(0 To lSize - 1) As Byte
  387.    Get #iFile, , bArray()
  388.    Close #iFile
  389.    
  390.      
  391.     LoadJpegFileIntoArray = True
  392. Exit_CmdLoad_Click:
  393.     Exit Function
  394.  
  395. Err_CmdLoad_Click:
  396. LoadJpegFileIntoArray = False
  397.     MsgBox Err.Description
  398.     Resume Exit_CmdLoad_Click
  399.    
  400. End Function
  401.  
  402.  
  403. Public Property Get JPegAsByteArray() As Variant
  404. JPegAsByteArray = bArray
  405.  
  406. End Property
  407.  
  408. Public Property Get hdc() As Long
  409.   hdc = m_hDC
  410. End Property
  411.  
  412.  
  413. Public Property Get hDib() As Long
  414.   hDib = m_hDib
  415. End Property
  416.  
  417.  
  418. Public Property Get DIBSectionBitsPtr() As Long
  419.   DIBSectionBitsPtr = m_lPtr
  420. End Property
  421.  
  422.  
  423. Public Function DIBtoPictureData(ctl As Control)
  424.  Dim lngRet As Long
  425.  Dim ds As DIBSECTION
  426.      
  427.      lngRet = apiGetObject(hDib, Len(ds), ds)
  428.      
  429.     '.bfSize = Len(FileHeader) + Len(ds.dsBmih) + ds.dsBmih.biSizeImage
  430.        
  431.     ' Update the Image Control display
  432.    ' We do this by simply copying the mBitmapAdd's contents to
  433.    ' the control's PictureData prop
  434.    
  435.     Dim varTemp() As Byte
  436.     ReDim varTemp(ds.dsBmih.biSizeImage + 40)
  437.     apiCopyMemory varTemp(40), ByVal Me.DIBSectionBitsPtr, ds.dsBmih.biSizeImage
  438.     apiCopyMemory varTemp(0), ds.dsBmih, 40
  439.    
  440.      ctl.PictureData = varTemp
  441.  
  442.  
  443. End Function
  444.  
  445. Public Sub CleanUp()
  446.  
  447.   If (m_hDC <> 0) Then
  448.     If (m_hDib <> 0) Then
  449.       Call SelectObject(m_hDC, m_hBmpOld)
  450.       Call DeleteObject(m_hDib)
  451.     End If
  452.     Call DeleteObject(m_hDC)
  453.   End If
  454.  
  455.   m_hDC = 0
  456.   m_hDib = 0
  457.   m_hBmpOld = 0
  458.   m_lPtr = 0
  459.  
  460.   m_bmi.bmiColors.rgbBlue = 0
  461.   m_bmi.bmiColors.rgbGreen = 0
  462.   m_bmi.bmiColors.rgbRed = 0
  463.   m_bmi.bmiColors.rgblReterved = 0
  464.   m_bmi.bmiHeader.biSize = Len(m_bmi.bmiHeader)
  465.   m_bmi.bmiHeader.biWidth = 0
  466.   m_bmi.bmiHeader.biHeight = 0
  467.   m_bmi.bmiHeader.biPlanes = 0
  468.   m_bmi.bmiHeader.biBitCount = 0
  469.   m_bmi.bmiHeader.biClrUsed = 0
  470.   m_bmi.bmiHeader.biClrImportant = 0
  471.   m_bmi.bmiHeader.biCompression = 0
  472.  
  473. End Sub
  474.  
  475.  
  476. Private Sub Class_Terminate()
  477.   CleanUp
  478. End Sub
  479.  
  480.  
  481. Public Function FileDialog(LoadSave As Boolean) As String
  482. ' Calls the API File Dialog Window
  483. ' Returns full path to new File.
  484. ' If LoadSave = TRUE then call File Load Dialog
  485.  
  486. On Error GoTo Err_fFileDialog
  487.  
  488. ' Call the File Common Dialog Window
  489. Dim clsDialog As Object
  490. Dim strTemp As String
  491. Dim strfName As String
  492.  
  493. Set clsDialog = New clsCommonDialog
  494.  
  495. ' Fill in our structure
  496. ' I'll leave in how to select Jpeg to
  497. ' show you how to build the Filter
  498. clsDialog.Filter = "JPEG (*.JPG)" & Chr$(0) & "*.JPG" & Chr$(0)
  499. clsDialog.Filter = clsDialog.Filter & "Jpe (*.JPE)" & Chr$(0) & "*.JPE" & Chr$(0)
  500. clsDialog.Filter = clsDialog.Filter & "Jpeg (*.JPEG)" & Chr$(0) & "*.JPEG" & Chr$(0)
  501. clsDialog.Filter = clsDialog.Filter & "ALL (*.*)" & Chr$(0) & "*.*" & Chr$(0)
  502.  
  503. 'clsDialog.Filter = clsDialog.Filter & "Gif (*.GIF)" & Chr$(0) & "*.GIF" & Chr$(0)
  504.  
  505.  
  506. If LoadSave Then
  507. ' Display the Open File Dialog
  508. clsDialog.DialogTitle = "Please Select a JPEG File to Load"
  509. clsDialog.ShowOpen
  510. Else
  511. clsDialog.DialogTitle = "Please Enter/Select a FileName to save the JPEG File"
  512. clsDialog.ShowSave
  513. End If
  514.  
  515. ' See if user clicked Cancel or even selected
  516. ' the very same file already selected
  517. strfName = clsDialog.FileName
  518. If Len(strfName & vbNullString) = 0 Then
  519. Set clsDialog = Nothing
  520. Exit Function
  521. '' Raise the exception
  522. ' Err.Raise vbObjectError + 513, "clsPrintToFit.fFileDialog", _
  523.  ' "Please type in a Name for a New File"
  524. End If
  525.  
  526. ' Return File Path and Name
  527. FileDialog = strfName
  528. ' Update our property
  529. m_CurrentJpegFileName = strfName
  530.  
  531. Exit_fFileDialog:
  532.  
  533. Err.Clear
  534. Set clsDialog = Nothing
  535. Exit Function
  536.  
  537. Err_fFileDialog:
  538. FileDialog = ""
  539. m_CurrentJpegFileName = ""
  540. MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
  541. Resume Exit_fFileDialog
  542.  
  543. End Function
  544.  
  545.  
  546.  
  547. Public Function WMFtoBMP(bWMF() As Byte, mm As Long, xExt As Long, yExt As Long) As Boolean
  548. Dim hEMF As Long
  549. Dim lngIC As Long
  550.  
  551. ' Instance of EMF Header structure
  552. Dim mh As ENHMETAHEADER
  553.  
  554. ' Current Screen Resolution
  555. Dim lngXdpi As Long
  556. Dim lngYdpi As Long
  557.  
  558. ' Used to convert Metafile dimensions to pixels
  559. Dim sngConvertX As Single
  560. Dim sngConvertY As Single
  561. Dim sngMetaResolutionX As Single
  562. Dim sngMetaResolutionY As Single
  563.  
  564. Dim rc As RECT
  565.  
  566. Dim mfp As METAFILEPICT
  567.  
  568.  
  569. ' Init our vars
  570.  CleanUp
  571.  
  572. ' Convert EMF byte array to memory EMF
  573. With mfp
  574.     .hMF = 0
  575.     .mm = mm
  576.     .xExt = xExt
  577.     .yExt = yExt
  578. End With
  579.  
  580. hEMF = SetWinMetaFileBits(UBound(bWMF) + 1, bWMF(0), 0&, mfp)
  581. If hEMF = 0 Then
  582.     'Call DeleteObject(m_hDC)
  583.    'm_hDC = 0
  584.    WMFtoBMP = False
  585.     Exit Function
  586. End If
  587.  
  588. ' Convert EMF size to pixels
  589. '
  590. lngRet = GetEnhMetaFileHeader(hEMF, Len(mh), mh)
  591. If lngRet = 0 Then
  592.     WMFtoBMP = False
  593.     Exit Function
  594. End If
  595.  
  596. With mh.rclFrame
  597.     ' The rclFrame member Specifies the dimensions,
  598.    ' in .01 millimeter units, of a rectangle that surrounds
  599.    ' the picture stored in the metafile.
  600.    ' I'll show this as seperate steps to aid in understanding
  601.    ' the conversion process.
  602.    
  603. ' Convert to MM
  604. sngConvertX = (.right - .Left) * 0.01
  605. sngConvertY = (.Bottom - .top) * 0.01
  606.  End With
  607.  
  608. ' Convert to CM
  609. sngConvertX = sngConvertX * 0.1
  610. sngConvertY = sngConvertY * 0.1
  611. ' Convert to Inches
  612. sngConvertX = sngConvertX / 2.54
  613. sngConvertY = sngConvertY / 2.54
  614.  
  615.  
  616. ' Get current Screen DPI
  617. lngIC = apiCreateIC("DISPLAY", vbNullString, vbNullString, vbNullString)
  618. 'If the call to CreateIC didn't fail, then get the Screen X resolution.
  619. If lngIC <> 0 Then
  620.     lngXdpi = apiGetDeviceCaps(lngIC, LOGPIXELSX)
  621.     lngYdpi = apiGetDeviceCaps(lngIC, LOGPIXELSY)
  622.     'Release the information context.
  623.    apiDeleteDC (lngIC)
  624. Else
  625.     ' Something has gone wrong. Assume an average value.
  626.    lngXdpi = 120
  627.     lngYdpi = 120
  628. End If
  629.  
  630. ' Convert the szlMillimeters to inches. This member
  631. ' Specifies the resolution of the reference device, in millimeters.
  632. ' Convert Inches to Pixels
  633. 'sngMetaResolutionX = (mh.szlMillimeters.cx * 0.01) / 2.54
  634. sngMetaResolutionX = (mh.szlDevice.cx / ((mh.szlMillimeters.cx * 0.1) / 2.54))
  635. sngMetaResolutionY = (mh.szlDevice.cy / ((mh.szlMillimeters.cy * 0.1) / 2.54))
  636.  
  637. Create CLng(sngConvertX * sngMetaResolutionX), CLng(sngConvertY * sngMetaResolutionY)
  638.  
  639. ' **********************
  640. ' I have seen cases where the xExt and yExt values are not correct.
  641. ' I may consider playing the MWF into an EMF DC so that
  642. ' I could allow the GDI to determine the
  643. ' actual extents of the Image. Next revision.
  644.  
  645.  
  646. ' Case CF_ENHMETAFILE
  647. ' If it is an Enhanced Metafile then we
  648. ' Need to  "PLAY" the Metafile
  649. ' back into the Device COntext instead
  650. ' of using the SelectObject API
  651.  
  652. rc.top = 0
  653. rc.Left = 0
  654. rc.Bottom = m_bmi.bmiHeader.biHeight
  655. rc.right = m_bmi.bmiHeader.biWidth
  656. lngRet = apiPlayEnhMetaFile(m_hDC, hEMF, rc)
  657.  
  658. ' Delete the EMF
  659. lngRet = apiDeleteEnhMetaFile(hEMF)
  660.  
  661. ' Resize array
  662. GetDIBBytes bWMF()
  663.    
  664. '// Success
  665. WMFtoBMP = True
  666. End Function
  667.  
  668.  
  669.  
  670. Public Function GetDIBBytes(bBytes() As Byte)
  671. Dim lngRet As Long
  672. Dim lSize As Long
  673.  
  674.  
  675. lSize = m_bmi.bmiHeader.biSizeImage - 1
  676. ReDim bBytes(0 To lSize) As Byte
  677.  
  678. apiCopyMemory bBytes(0), ByVal m_lPtr, m_bmi.bmiHeader.biSizeImage
  679.        
  680. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement