├── LICENSE ├── README.md ├── README_EN.md ├── clsMemDC.cls └── clsMemDC_EN.cls /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Hanson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VB6 MemoryDC 2 | [English Version](README_EN.md) 3 | 4 | 一个VB6编写的内存图类,让你可以更简单的操作内存图。 5 | 6 | # 使用方法 7 | 8 | ## 初始化内存图类 9 | 10 | 声明一个 MemoryDC 类型的变量,只需要 11 | ```VBS 12 | Dim memDC As New clsMemDC 13 | ``` 14 | 15 | 接着,在使用内存图前创建它: 16 | ```VBS 17 | memDC.CreateMemDC image_width,image_height 18 | ``` 19 | 对于函数 `CreateMemDC`,请参考“函数说明”部分中对于它的描述。 20 | 21 | ## 删除内存图类 22 | 23 | 当你不再需要内存图类的时候,只需要 24 | ```VBS 25 | memDC.DeleteMemDC 26 | Set memDC = Nothing 27 | ``` 28 | 29 | ## 从其它的DC绘图过来 30 | 31 | 从其他的DC绘制图像过来,只需要 32 | ```VBS 33 | memDC.BitBltFrom YourDC, FromX, FromY, ToX, ToY, image_width, image_height 34 | ``` 35 | 36 | 对于函数 `BitBltFrom`,请参考“函数说明”部分中对于它的描述。 37 | 38 | ## 绘图到其他的DC 39 | 40 | 从内存图绘制图像到其他DC,只需要 41 | ```VBS 42 | memDC.BitBltTo YourDC, ToX, ToY, FromX, FromY, image_width, image_height 43 | ``` 44 | 45 | 对于函数 `BitBltTo`,请参考“函数说明”部分中对于它的描述。 46 | 47 | ## 复制字节数据到Byte数组 48 | 49 | 把内存图的数据复制到一个Byte数组里,只需要 50 | ```VBS 51 | memDC.CopyDataTo data 'data 是一个 Byte() 类型的数组变量 52 | ``` 53 | 54 | 对于函数 `CopyDataTo`,请参考“函数说明”部分中对于它的描述。 55 | 56 | ## 从Byte数组复制字节数据到内存图像 57 | 58 | 把Byte数组中的数据复制到内存图像,只需要 59 | ```VBS 60 | memDC.CopyDataFrom data 'data 是一个 Byte() 类型的数组变量 61 | ``` 62 | 63 | 对于函数 `CopyDataFrom`,请参考“函数说明”部分中对于它的描述。 64 | 65 | ## 其他功能请参考下面的部分。 66 | 67 | # 函数说明 68 | 69 | ## `CreateMemDC` 函数 70 | 71 | `CreateMemDC` 函数先设置内存图像的属性(宽度,高度,颜色位数),再创建它。 72 | 73 | ### 定义 74 | 75 | ```VBS 76 | Public Function CreateMemDC(ByVal iWidth As Long, ByVal iHeight As Long, _ 77 | Optional ByVal iBitCount As Integer = 16, Optional ByVal FromHdc As Long = 0) As Boolean 78 | ``` 79 | 80 | ### 参数 81 | 82 | `iWidth`: Long, 需要创建的内存图像的宽度。 83 | 84 | `iHeight`: Long, 需要创建的内存图像的高度。 85 | 86 | `iBitCount`: 可选的, Integer, 需要创建的内存图像的颜色位数。默认是16位。 87 | 88 | `FromHdc`: 可选的, Long, 源DC句柄。默认为0。 89 | 90 | ### 返回值 91 | 92 | 类型: `Boolean` 93 | 94 | 如果内存图像成功创建,则函数返回`True`。否则,函数返回`False`。 95 | 96 | ### 例子 97 | 98 | ```VBS 99 | memDC.CreateMemDC 1920, 1080 '创建一个 1920 * 1080 的内存图像 100 | ``` 101 | 102 | ```VBS 103 | memDC.CreateMemDC 1920, 1080, 8 '创建一个 1920 * 1080,8位 的内存图像 104 | ``` 105 | 106 | ```VBS 107 | memDC.CreateMemDC 1920, 1080, , frmMain.hDC '从frmMain.hDC 创建一个 1920 * 1080 的内存图像 108 | ``` 109 | 110 | **注意:当您使用 `CreateMemDC` 函数的时候,这个函数会首先自动删除掉之前创建的内存图像** 111 | 112 | ## `DeleteMemDC` 函数 113 | 114 | `DeleteMemDC` 函数可以删掉创建的内存图像。 115 | 116 | ### 定义 117 | 118 | ```VBS 119 | Public Sub DeleteMemDC() 120 | ``` 121 | 122 | ### 例子 123 | 124 | ```VBS 125 | memDC.DeleteMemDC 126 | ``` 127 | 128 | **注意:当您不再需要内存图像的时候应调用这个函数。当类被销毁的时候,这个函数会自动调用。** 129 | 130 | ## `BitBltFrom` 函数 131 | 132 | `BitBltFrom` 函数可以从其他DC绘制图像到内存图里。 133 | 134 | ### 定义 135 | 136 | ```VBS 137 | Public Function BitBltFrom(FromHdc As Long, FromX As Long, FromY As Long, _ 138 | ToX As Long, ToY As Long, iWidth As Long, iHeight As Long, _ 139 | Optional DrawMode As RasterOpConstants = vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS) As Boolean 140 | ``` 141 | ### 参数 142 | 143 | `FromHdc`: Long, 指定来源图像的DC句柄。 144 | 145 | `FromX`: Long, 原图像的X位置。 146 | 147 | `FromY`: Long, 原图像的Y位置。 148 | 149 | `ToX`: Long, 目标图像的X位置。 150 | 151 | `ToY`: Long, 目标图像的Y位置。 152 | 153 | `iWidth`: Long, 需要绘制的图像的宽度。 154 | 155 | `iHeight`: Long, 需要绘制的图像的高度。 156 | 157 | `DrawMode`: 可选的, RasterOpConstants, 指定绘图的模式。 默认是 `vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS`。 绘图模式可以使以下常数的组合: 158 | 159 | | RasterOpConstants | 160 | |-------------------| 161 | | vbDstInvert | 162 | | vbMergeCopy | 163 | | vbMergePaint | 164 | | vbNotSrcCopy | 165 | | vbNotSrcErase | 166 | | vbPatCopy | 167 | | vbPatInvert | 168 | | vbPatPaint | 169 | | vbSrcAnd | 170 | | vbSrcCopy | 171 | | vbSrcErase | 172 | | vbSrcInvert | 173 | | vbSrcPaint | 174 | 175 | ### 返回值 176 | 177 | 类型: `Boolean` 178 | 179 | 如果图像成功绘制,则函数返回`True`。否则,函数返回`False`。 180 | 181 | ### 例子 182 | 183 | ```VBS 184 | memDC.BitBltFrom frmMain.hDC, 0, 0, 0, 0, 100, 100 '从 frmMain.hDC 句柄, 窗体的(0, 0)绘制到内存图像的(0, 0),大小为 100 * 100 185 | ``` 186 | 187 | ```VBS 188 | memDC.BitBltFrom GetDC(0), 100, 200, 150, 250, 300, 400, vbSrcInvert '从屏幕DC句柄,屏幕的(100, 200)绘制到内存图像的(150, 250),大小为 300 * 400,使用 vbSrcInvert 绘图模式 189 | ``` 190 | 191 | ## `BitBltTo` 函数 192 | 193 | `BitBltTo` 函数可以从内存图绘制图像到其他DC里。 194 | 195 | ### 定义 196 | 197 | ```VBS 198 | Public Function BitBltTo(ToHdc As Long, ToX As Long, ToY As Long, _ 199 | FromX As Long, FromY As Long, iWidth As Long, iHeight As Long, _ 200 | Optional DrawMode As RasterOpConstants = vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS) As Boolean 201 | ``` 202 | ### 参数 203 | 204 | `ToHdc`: Long, 指定绘画图像的目标DC句柄。 205 | 206 | `ToX`: Long, 目标图像的X位置。 207 | 208 | `ToY`: Long, 目标图像的Y位置。 209 | 210 | `FromX`: Long, 原图像的X位置。 211 | 212 | `FromY`: Long, 原图像的Y位置。 213 | 214 | `iWidth`: Long, 需要绘制的图像的宽度。 215 | 216 | `iHeight`: Long, 需要绘制的图像的高度。 217 | 218 | `DrawMode`: 可选的,RasterOpConstants,指定绘图的模式。 默认是 `vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS`。对于DrawMode可用的常数值,请参考"`BitBltFrom` 函数"部分。 219 | 220 | ### 返回值 221 | 222 | 类型: `Boolean` 223 | 224 | 如果图像成功绘制,则函数返回`True`。否则,函数返回`False`。 225 | 226 | ### 例子 227 | 228 | ```VBS 229 | memDC.BitBltTo frmMain.hDC, 0, 0, 0, 0, 100, 100 '绘制图像到 frmMain.hDC 句柄,从内存图像的(0, 0)绘制到窗体的(0, 0),大小为 100 * 100 230 | ``` 231 | 232 | ```VBS 233 | memDC.BitBltTo GetDC(0), 100, 200, 150, 250, 300, 400, vbSrcInvert '绘制图像到屏幕DC句柄,从内存图像的(100, 200)绘制到屏幕的(150, 250) ,大小为 300 * 400, 使用 vbSrcInvert 绘图模式 234 | ``` 235 | 236 | ## `CopyDataFrom` 函数 237 | 238 | `CopyDataFrom` 函数从Byte数组复制数据到内存图像。 239 | 240 | ### 定义 241 | 242 | ```VBS 243 | Public Sub CopyDataFrom(FromArray() As Byte) 244 | ``` 245 | 246 | ### 参数 247 | 248 | `FromArray`: Byte(), 指定一个Byte数组作为复制数据的来源。 249 | 250 | ### 例子 251 | 252 | ```VBS 253 | memDC.CopyDataFrom data '从 data 复制图像数据,其中 data 是一个 Byte 数组 254 | ``` 255 | 256 | **注意: `CopyDataFrom` 复制整个数组的数据到内存图像的数据区里。因此,它会检查数组的大小是否大于内存图像的数据区大小。如果内存图像的数据区比数组的大小要小,那么这个函数只会复制等同于数据区大小的数据。例如,如果数组大小是10字节,内存图像数据区大小是5字节,那么这个函数只会从数组复制5字节的数据。** 257 | 258 | ## `CopyDataTo` 函数 259 | 260 | `CopyDataTo` 函数从内存图像的数据区复制数据到Byte数组里。 261 | 262 | ### 定义 263 | 264 | ```VBS 265 | Public Function CopyDataTo(ToArray() As Byte) As Boolean 266 | ``` 267 | 268 | ### 参数 269 | 270 | `ToArray`: Byte(), 指定一个数组作为复制数据的目标。 271 | 272 | ### 返回值 273 | 274 | 类型: `Boolean` 275 | 276 | 如果数据成功复制,那么函数返回 `True`。否则,函数返回`False`。 277 | 278 | ### 例子 279 | 280 | ```VBS 281 | memDC.CopyDataTo data '复制图像数据到 data 里,其中 data 是一个 Byte 数组 282 | ``` 283 | 284 | **注意: `CopyDataTo` 函数会复制所有内存图像的数据区里的数据到数组中。因此,它会检查数组大小是否足够存储内存图像的数据。如果内存图像的数据区大小比数组大,这个函数会失败。例如,如果数组的大小是5字节,内存图像的数据区大小是10字节,这个函数会返回`False`** 285 | 286 | # 属性说明 287 | 288 | `iWidth`: Long, 内存图像的宽度。 289 | 290 | `iHeight`: Long, 内存图像的高度。 291 | 292 | `iBitCount`: Long, 内存图像的颜色位数。 293 | 294 | `iImageSize`: Long, 内存图像的数据区大小。 295 | 296 | `hDC`: Long, 内存图像的DC句柄。 297 | 298 | `hBmp`: Long, 内存图像的位图句柄。该位图与内存DC一同创建。 299 | 300 | `lpBitData`: Long, 内存图像的数据区地址。 301 | 302 | # 开源协议 303 | 304 | MIT 305 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # VB6 MemoryDC 2 | A memory DC class written in VB6. You can use this class to create and operate memory DCs easily. 3 | 4 | # Usage 5 | 6 | ## Initalizing MemoryDC class 7 | 8 | To declare a MemoryDC typed variable, simply 9 | ```VBS 10 | Dim memDC As New clsMemDC 11 | ``` 12 | 13 | Next, you should create the DC when you need it: 14 | ```VBS 15 | memDC.CreateMemDC image_width, image_height 16 | ``` 17 | For function `CreateMemDC`, please refer its description in "Functions Description" section. 18 | 19 | ## Deleting MemoryDC class 20 | 21 | When you no longer need the MemoryDC typed variable, simply 22 | ```VBS 23 | memDC.DeleteMemDC 24 | Set memDC = Nothing 25 | ``` 26 | 27 | ## Painting from other DCs 28 | 29 | To paint the image from other DCs, simply 30 | ```VBS 31 | memDC.BitBltFrom YourDC, FromX, FromY, ToX, ToY, image_width, image_height 32 | ``` 33 | 34 | For function `BitBltFrom`, please refer its description in "Functions Description" section. 35 | 36 | ## Painting to other DCs 37 | 38 | To paint the image to other DCs, simply 39 | ```VBS 40 | memDC.BitBltTo YourDC, ToX, ToY, FromX, FromY, image_width, image_height 41 | ``` 42 | 43 | For function `BitBltTo`, please refer its description in "Functions Description" section. 44 | 45 | ## Copying bit data to a Byte array 46 | 47 | To copy the bit data of the image in the DC to a Byte array, simply 48 | ```VBS 49 | memDC.CopyDataTo data 'data is a Byte() typed array variable 50 | ``` 51 | 52 | For function `CopyDataTo`, please refer its description in "Functions Description" section. 53 | 54 | ## Copying bit data from a Byte array 55 | 56 | To copy the bit data of the image in the DC from a Byte array, simply 57 | ```VBS 58 | memDC.CopyDataFrom data 'data is a Byte() typed array variable 59 | ``` 60 | 61 | For function `CopyDataFrom`, please refer its description in "Functions Description" section. 62 | 63 | ## For more functionalities, please read the following sections. 64 | 65 | # Functions Description 66 | 67 | ## `CreateMemDC` function 68 | 69 | `CreateMemDC` function sets the memory DC information (width, height, bit count) and creates it. 70 | 71 | ### Definition 72 | 73 | ```VBS 74 | Public Function CreateMemDC(ByVal iWidth As Long, ByVal iHeight As Long, _ 75 | Optional ByVal iBitCount As Integer = 16, Optional ByVal FromHdc As Long = 0) As Boolean 76 | ``` 77 | 78 | ### Parameters 79 | 80 | `iWidth`: Long, the width of the memory DC being created. 81 | 82 | `iHeight`: Long, the height of the memory DC being created. 83 | 84 | `iBitCount`: Optional, Integer, the color bit count of the memory DC being created. Default is 16 bit. 85 | 86 | `FromHdc`: Optional, Long, the source DC handle. Default is 0. 87 | 88 | ### Return value 89 | 90 | Type: `Boolean` 91 | 92 | If the memory DC is created successfully, the function returns `True`. Otherwise, the function returns `False`. 93 | 94 | ### Examples 95 | 96 | ```VBS 97 | memDC.CreateMemDC 1920, 1080 'Creates a 1920 * 1080 memory DC 98 | ``` 99 | 100 | ```VBS 101 | memDC.CreateMemDC 1920, 1080, 8 'Creates a 1920 * 1080, 8 bit memory DC 102 | ``` 103 | 104 | ```VBS 105 | memDC.CreateMemDC 1920, 1080, , frmMain.hDC 'Creates a 1920 * 1080 memory DC from frmMain's hDC 106 | ``` 107 | 108 | **NOTE: When you use `CreateMemDC` function, this function deletes the previous memory DC automatically.** 109 | 110 | ## `DeleteMemDC` function 111 | 112 | `DeleteMemDC` deletes the create memory DC. 113 | 114 | ### Definition 115 | 116 | ```VBS 117 | Public Sub DeleteMemDC() 118 | ``` 119 | 120 | ### Examples 121 | 122 | ```VBS 123 | memDC.DeleteMemDC 124 | ``` 125 | 126 | **NOTE: Call `DeleteMemDC` when you don't need the memory DC anymore. This function is automatically called when the class is being terminated.** 127 | 128 | ## `BitBltFrom` function 129 | 130 | `BitBltFrom` paints image from other DCs to the created memory DC. 131 | 132 | ### Definition 133 | 134 | ```VBS 135 | Public Function BitBltFrom(FromHdc As Long, FromX As Long, FromY As Long, _ 136 | ToX As Long, ToY As Long, iWidth As Long, iHeight As Long, _ 137 | Optional DrawMode As RasterOpConstants = vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS) As Boolean 138 | ``` 139 | ### Parameters 140 | 141 | `FromHdc`: Long, Specifics the DC handle to paint image from. 142 | 143 | `FromX`: Long, X position of the original image. 144 | 145 | `FromY`: Long, Y position of the original image. 146 | 147 | `ToX`: Long, X position of the target image. 148 | 149 | `ToY`: Long, Y position of the target image. 150 | 151 | `iWidth`: Long, width of the image being painted 152 | 153 | `iHeight`: Long, height of the image being painted 154 | 155 | `DrawMode`: Optional, RasterOpConstants, specifics painting mode. Default is `vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS`. Painting mode can be the combination of the following constants: 156 | 157 | | RasterOpConstants | 158 | |-------------------| 159 | | vbDstInvert | 160 | | vbMergeCopy | 161 | | vbMergePaint | 162 | | vbNotSrcCopy | 163 | | vbNotSrcErase | 164 | | vbPatCopy | 165 | | vbPatInvert | 166 | | vbPatPaint | 167 | | vbSrcAnd | 168 | | vbSrcCopy | 169 | | vbSrcErase | 170 | | vbSrcInvert | 171 | | vbSrcPaint | 172 | 173 | ### Return value 174 | 175 | Type: `Boolean` 176 | 177 | If the image is painted successfully, the function returns `True`. Otherwise, the function returns `False`. 178 | 179 | ### Examples 180 | 181 | ```VBS 182 | memDC.BitBltFrom frmMain.hDC, 0, 0, 0, 0, 100, 100 'Paints the image from frmMain.hDC, from (0, 0) of the window to (0, 0) of the memory DC, sized 100 * 100 183 | ``` 184 | 185 | ```VBS 186 | memDC.BitBltFrom GetDC(0), 100, 200, 150, 250, 300, 400, vbSrcInvert 'Paints the image from the screen DC, from (100, 200) of the screen to (150, 250) of the memory DC, sized 300 * 400, using vbSrcInvert painting mode 187 | ``` 188 | 189 | ## `BitBltTo` function 190 | 191 | `BitBltTo` paints image from the created memory DC to other DCs. 192 | 193 | ### Definition 194 | 195 | ```VBS 196 | Public Function BitBltTo(ToHdc As Long, ToX As Long, ToY As Long, _ 197 | FromX As Long, FromY As Long, iWidth As Long, iHeight As Long, _ 198 | Optional DrawMode As RasterOpConstants = vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS) As Boolean 199 | ``` 200 | ### Parameters 201 | 202 | `ToHdc`: Long, Specifics the DC handle to paint image to. 203 | 204 | `ToX`: Long, X position of the target image. 205 | 206 | `ToY`: Long, Y position of the target image. 207 | 208 | `FromX`: Long, X position of the original image. 209 | 210 | `FromY`: Long, Y position of the original image. 211 | 212 | `iWidth`: Long, width of the image being painted 213 | 214 | `iHeight`: Long, height of the image being painted 215 | 216 | `DrawMode`: Optional, RasterOpConstants, specifics painting mode. Default is `vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS`. For constants available for DrawMode, please refer to "`BitBltFrom` function" section. 217 | 218 | ### Return value 219 | 220 | Type: `Boolean` 221 | 222 | If the image is painted successfully, the function returns `True`. Otherwise, the function returns `False`. 223 | 224 | ### Examples 225 | 226 | ```VBS 227 | memDC.BitBltTo frmMain.hDC, 0, 0, 0, 0, 100, 100 'Paints the image to frmMain.hDC, from (0, 0) of the memory DC to (0, 0) of the window, sized 100 * 100 228 | ``` 229 | 230 | ```VBS 231 | memDC.BitBltTo GetDC(0), 100, 200, 150, 250, 300, 400, vbSrcInvert 'Paints the image to the screen DC, from (100, 200) of the memory DC to (150, 250) of the screen DC, sized 300 * 400, using vbSrcInvert painting mode 232 | ``` 233 | 234 | ## `CopyDataFrom` function 235 | 236 | `CopyDataFrom` function copies data from a Byte array to the memory DC. 237 | 238 | ### Definition 239 | 240 | ```VBS 241 | Public Sub CopyDataFrom(FromArray() As Byte) 242 | ``` 243 | 244 | ### Parameters 245 | 246 | `FromArray`: Byte(), the Byte array to copy data from. 247 | 248 | ### Examples 249 | 250 | ```VBS 251 | memDC.CopyDataFrom data 'Copy image data from data, where data is a Byte array 252 | ``` 253 | 254 | **NOTE: `CopyDataFrom` function copies all data from the array to the memory region of the memory DC. So it checks if the array size is larger than the size of memory region of the memory DC or not. If the memory region is smaller than the size of the array, it only copies data in the same size to the size of memory region of the memory DC. For example, if the array is 10 bytes, and the size of the memory region of the memory DC is 5 bytes, this function will only copy 5 bytes from the array.** 255 | 256 | ## `CopyDataTo` function 257 | 258 | `CopyDataTo` function copies data from the memory DC to a Byte array. 259 | 260 | ### Definition 261 | 262 | ```VBS 263 | Public Function CopyDataTo(ToArray() As Byte) As Boolean 264 | ``` 265 | 266 | ### Parameters 267 | 268 | `ToArray`: Byte(), the Byte array to copy data to. 269 | 270 | ### Return value 271 | 272 | Type: `Boolean` 273 | 274 | If the data is copied successfully, the function returns `True`. Otherwise, the function returns `False`. 275 | 276 | ### Examples 277 | 278 | ```VBS 279 | memDC.CopyDataTo data 'Copy image data to data, where data is a Byte array 280 | ``` 281 | 282 | **NOTE: `CopyDataTo` function copies all data from the memory region of the memory DC to the array. So it checks if the array size is large enough to receive the memory DC data or not. If the memory region is larger than the size of the array, this function fails. For example, if the array is 5 bytes, and the size of the memory region of the memory DC is 10 bytes, this function will return `False`** 283 | 284 | # Properties Description 285 | 286 | `iWidth`: Long, the width of the memory DC. 287 | 288 | `iHeight`: Long, the height of the memory DC. 289 | 290 | `iBitCount`: Long, the color bit count of the memory DC. 291 | 292 | `iImageSize`: Long, the size of the memory region of the memory DC. 293 | 294 | `hDC`: Long, the handle to the created memory DC. 295 | 296 | `hBmp`: Long, the handle to the bitmap created. This bitmap is created together with the memory DC. 297 | 298 | `lpBitData`: Long, the address of the memory region of the memory DC. 299 | 300 | # License 301 | 302 | MIT 303 | -------------------------------------------------------------------------------- /clsMemDC.cls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SweetIceLolly/VB6-MemoryDC/75499e754efecfa0aae11d66348dd5cdee0365ee/clsMemDC.cls -------------------------------------------------------------------------------- /clsMemDC_EN.cls: -------------------------------------------------------------------------------- 1 | VERSION 1.0 CLASS 2 | BEGIN 3 | MultiUse = -1 'True 4 | Persistable = 0 'NotPersistable 5 | DataBindingBehavior = 0 'vbNone 6 | DataSourceBehavior = 0 'vbNone 7 | MTSTransactionMode = 0 'NotAnMTSObject 8 | END 9 | Attribute VB_Name = "clsMemDC" 10 | Attribute VB_GlobalNameSpace = False 11 | Attribute VB_Creatable = True 12 | Attribute VB_PredeclaredId = False 13 | Attribute VB_Exposed = False 14 | Option Explicit 15 | 16 | 'Binding bitmap with DC 17 | Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long 18 | 'Create DC that compatible to current device 19 | Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long 20 | 'Create Bitmap that compatible to specified DC 21 | Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hDC As Long, _ 22 | pBitmapInfo As BITMAPINFO, ByVal un As Long, ByVal lplpVoid As Long, _ 23 | ByVal handle As Long, ByVal dw As Long) As Long 24 | 'Delete DC 25 | Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long 26 | 'Delete Bitmap 27 | Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long 28 | 'Paint 29 | Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _ 30 | ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _ 31 | ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, _ 32 | ByVal ySrc As Long, ByVal dwRop As Long) As Long 33 | 'Copy memory 34 | Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _ 35 | Destination As Any, Source As Any, ByVal Length As Long) 36 | 37 | 'Bitmap info header 38 | Private Type BITMAPINFOHEADER 39 | biSize As Long 40 | biWidth As Long 41 | biHeight As Long 42 | biPlanes As Integer 43 | biBitCount As Integer 44 | biCompression As Long 45 | biSizeImage As Long 46 | biXPelsPerMeter As Long 47 | biYPelsPerMeter As Long 48 | biClrUsed As Long 49 | biClrImportant As Long 50 | End Type 51 | 52 | 'RGB color table 53 | Private Type RGBQUAD 54 | rgbBlue As Byte 55 | rgbGreen As Byte 56 | rgbRed As Byte 57 | rgbReserved As Byte 58 | End Type 59 | 60 | 'Bitmap info 61 | Private Type BITMAPINFO 62 | bmiHeader As BITMAPINFOHEADER 63 | bmiColors As RGBQUAD 64 | End Type 65 | 66 | Private Const DIB_RGB_COLORS = 0 'Color table 67 | Private Const BITBLT_TRANSPARENT_WINDOWS = &H40000000 'Enables to capture transparent windows 68 | 69 | Private bi As BITMAPINFO 'Bitmap info 70 | Private hhDC As Long 'Memory DC handle 71 | Private hhBmp As Long 'Memory Bitmap handle 72 | Private lpData As Long 'Pointer to the bitmap data 73 | Private bSize As Long 'Memory size of the Bitmap data (in bytes) 74 | 75 | 'Bitmap width 76 | Public Property Get iWidth() As Long 77 | iWidth = bi.bmiHeader.biWidth 78 | End Property 79 | 80 | 'Bitmap height 81 | Public Property Get iHeight() As Long 82 | iHeight = bi.bmiHeader.biHeight 83 | End Property 84 | 85 | 'Bitmap color bit count 86 | Public Property Get iBitCount() As Integer 87 | iBitCount = bi.bmiHeader.biBitCount 88 | End Property 89 | 90 | 'Bitmap data size (in bytes) 91 | Public Property Get iImageSize() As Long 92 | iImageSize = bi.bmiHeader.biSizeImage 93 | End Property 94 | 95 | 'Handle to the created DC 96 | Public Property Get hDC() As Long 97 | hDC = hhDC 98 | End Property 99 | 100 | 'Handle to the created Bitmap 101 | Public Property Get hBmp() As Long 102 | hBmp = hhBmp 103 | End Property 104 | 105 | 'Pointer to the Bitmap data 106 | Public Property Get lpBitData() As Long 107 | lpBitData = lpData 108 | End Property 109 | 110 | 'To create memory DC 111 | 'Args: Width, Height: Width and height of the memory DC£¨In pixels£©, respectively 112 | ' BitCount: Color bit count£¬can be 0, 1, 4, 8, 16, 24, 32. For jpg or png format, color bit should be 0 113 | ' hDCfrom: Create DC that compatibles to the specified DC, default = 0 114 | ' 115 | 'Return: True if succeed, False if failed 116 | Public Function CreateMemDC(ByVal iWidth As Long, ByVal iHeight As Long, _ 117 | Optional ByVal iBitCount As Integer = 16, Optional ByVal FromHdc As Long = 0) As Boolean 118 | 119 | 'Delete previous memory DC and Bitmap 120 | If hhDC <> 0 Or hhBmp <> 0 Then 121 | Call DeleteMemDC 122 | End If 123 | 124 | 'Set info of the Bitmap 125 | With bi.bmiHeader 126 | .biBitCount = iBitCount 127 | .biWidth = iWidth 128 | .biHeight = iHeight 129 | .biSize = Len(bi) 130 | .biPlanes = 1 131 | .biSizeImage = .biWidth * .biHeight * .biBitCount / 8 132 | bSize = .biSizeImage 133 | End With 134 | 135 | 'Create memory DC 136 | hhDC = CreateCompatibleDC(FromHdc) 137 | 138 | 'Create memory Bitmap 139 | hhBmp = CreateDIBSection(hhDC, bi, DIB_RGB_COLORS, ByVal VarPtr(lpData), 0, 0) 140 | 141 | 'Bind Bitmap and DC 142 | SelectObject hhDC, hhBmp 143 | 144 | CreateMemDC = (hhBmp <> 0) 145 | End Function 146 | 147 | 'To delete created memory DC and Bitmap 148 | Public Sub DeleteMemDC() 149 | If hhDC <> 0 Then 150 | DeleteDC hhDC 151 | End If 152 | If hhBmp <> 0 Then 153 | DeleteObject hhBmp 154 | End If 155 | End Sub 156 | 157 | 'To paint from the specified DC to the created DC (Others -> Me) 158 | 'Args: FromHdc: Specific a DC handle 159 | ' FromX, FromY: X, Y position of the source, respectively 160 | ' ToX, ToY: X, Y position of the created DC, respectively 161 | ' iWidth, iHeight: Width and height of the bitmap, respectively 162 | ' DrawMode: Painting mode, default = vbSrcCopy 163 | 'Return: True if succeed, False if failed 164 | Public Function BitBltFrom(FromHdc As Long, FromX As Long, FromY As Long, _ 165 | ToX As Long, ToY As Long, iWidth As Long, iHeight As Long, _ 166 | Optional DrawMode As RasterOpConstants = vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS) As Boolean 167 | 168 | If hhDC <> 0 And hhBmp <> 0 Then 169 | BitBltFrom = BitBlt(hhDC, ToX, ToY, iWidth, iHeight, FromHdc, FromX, FromY, DrawMode) 170 | Else 171 | BitBltFrom = False 172 | End If 173 | End Function 174 | 175 | 'To paint to the specified DC (Me -> Others) 176 | 'Args: ToHdc: Specific a DC handle 177 | ' ToX, ToY: X, Y position of the target, respectively 178 | ' FromX, FromY: X, Y position of the created DC, respectively 179 | ' iWidth, iHeight: Width and height of the bitmap, respectively 180 | ' DrawMode: Painting mode, default = vbSrcCopy 181 | 'Return: True if succeed, False if failed 182 | Public Function BitBltTo(ToHdc As Long, ToX As Long, ToY As Long, _ 183 | FromX As Long, FromY As Long, iWidth As Long, iHeight As Long, _ 184 | Optional DrawMode As RasterOpConstants = vbSrcCopy Or BITBLT_TRANSPARENT_WINDOWS) As Boolean 185 | 186 | If hhDC <> 0 And hhBmp <> 0 Then 187 | BitBltTo = BitBlt(ToHdc, ToX, ToY, iWidth, iHeight, hhDC, FromX, FromY, DrawMode) 188 | Else 189 | BitBltTo = False 190 | End If 191 | End Function 192 | 193 | 'To copy the specified data to the data of created bitmap (Other data -> Me) 194 | 'Args: FromArray: Specified data 195 | Public Sub CopyDataFrom(FromArray() As Byte) 196 | 'Safety precaution: Check if Bitmap size is smaller than the array size 197 | If UBound(FromArray) + 1 < bi.bmiHeader.biSize Then 198 | CopyMemory ByVal lpData, FromArray(0), ByVal UBound(FromArray) + 1 199 | Else 200 | CopyMemory ByVal lpData, FromArray(0), ByVal bi.bmiHeader.biSizeImage 201 | End If 202 | End Sub 203 | 204 | 'To copy the data of created bitmap to the specified data region (Me -> Other data) 205 | 'Args: ToArray: Specified data region. Note: The array must be large enough to contain the bitmap data 206 | 'Return: True if succeed, False if failed 207 | Public Function CopyDataTo(ToArray() As Byte) As Boolean 208 | 'Safety precaution: Check if the array is large enough to contain the bitmap data 209 | If UBound(ToArray) + 1 < bi.bmiHeader.biSizeImage Then 210 | CopyDataTo = False 211 | Exit Function 212 | End If 213 | 214 | CopyMemory ToArray(0), ByVal lpData, ByVal bi.bmiHeader.biSizeImage 215 | CopyDataTo = True 216 | End Function 217 | 218 | 'Release Bitmap and DC when the class is terminating 219 | Private Sub Class_Terminate() 220 | Call DeleteMemDC 221 | End Sub 222 | --------------------------------------------------------------------------------