读书人

请教如何保存超大尺寸图片到文件?用a

发布时间: 2012-01-13 22:43:29 作者: rapoo

请问怎么保存超大尺寸图片到文件?用api
Private Sub SaveBmp(hwnd, W As Long, H As Long)
Dim hRgn As Long
Dim hBrush As Long
Dim Dc As Long, Hbmp As Long
Dim BmpInfo As BitMapInfoHeader
Dim BmpFileHead As BITMAPFILEHEADER
Dim pData As Long
Dim L As Long
W = W / Screen.TwipsPerPixelX
H = H / Screen.TwipsPerPixelY
With BmpInfo
.biBitCount = 24
.biPlanes = 1
.biHeight = H
.biWidth = W
.biSize = 40 '本结构长度
End With
L = (W * 3 + 3) And &H7FFFFFFC
L = L * H

Hbmp = CreateDIBSection(GetDC(hwnd), BmpInfo, 0, pData, 0, 0)

old = SelectObject(hdc, Hbmp)
PrintWindow hwnd, hdc, 0
Dim buff() As Byte
ReDim buff(L - 1) As Byte
CopyMemory buff(0), ByVal pData, L

With BmpFileHead
.bfType(0) = 66: .bfType(1) = 77
.bfSize = 54 + L
.bfOffBits = 54
End With

L = FreeFile()
Open "ss.bmp" For Binary As L
Put L, 1, BmpFileHead
Put L, , BmpInfo
Put L, , buff()
Close L
SelectObject hdc, old
DeleteObject Hbmp
DeleteDC Dc

End Sub

我想保存一个超大窗体的图像到文件里,但是这个窗体太大了,像素高度和宽度都有10000多像素,我想问问怎么才能使内存不溢出,就在ReDim buff(L - 1) As Byte这句里面,每次都会内存溢出,或者有时候CreateDIBSection这个函数也会出错,如果尺寸小点的图像保存起来就完全没问题,请问有办法解决么 ?谢谢

[解决办法]
你招鬼呢?

图象方面,貌似laviewpbt(http://blog.csdn.net/laviewpbt)是很有研究的,等他来看看吧.
[解决办法]
给个思路:每次仅保存比如10个像素高的一横条,循环保存1000次即可在使用内存较少的条件下保存10000像素高的bmp了。
[解决办法]
jpg恐怕不行
bmp一定要用不带压缩的格式。
[解决办法]
CreateDIBSection
The CreateDIBSection function creates a device-independent bitmap (DIB) that applications can write to directly. The function gives you a pointer to the location of the bitmap's bit values. You can supply a handle to a file mapping object that the function will use to create the bitmap, or you can let the system allocate the memory for the bitmap.

HBITMAP CreateDIBSection(
HDC hdc, // handle to device context
CONST BITMAPINFO *pbmi,
// pointer to structure containing bitmap size,
// format, and color data
UINT iUsage, // color data type indicator: RGB values or
// palette indexes
VOID *ppvBits, // pointer to variable to receive a pointer to
// the bitmap's bit values
HANDLE hSection, // optional handle to a file mapping object
DWORD dwOffset // offset to the bitmap bit values within the
// file mapping object
);

Parameters
hdc
Handle to a device context. If the value of iUsage is DIB_PAL_COLORS, the function uses this device context's logical palette to initialize the device-independent bitmap's colors.
pbmi
Pointer to a BITMAPINFO structure that specifies various attributes of the device-independent bitmap, including the bitmap's dimensions and colors.
iUsage
Specifies the type of data contained in the bmiColors array member of the BITMAPINFO structure pointed to by pbmi (either logical palette indexes or literal RGB values). The following values are defined. Value Meaning


DIB_PAL_COLORS The bmiColors member is an array of 16-bit indexes into the logical palette of the device context specified by hdc.
DIB_RGB_COLORS The BITMAPINFO structure contains an array of literal RGB values.


ppvBits
Pointer to a variable that receives a pointer to the location of the device-independent bitmap's bit values.
hSection
Handle to a file mapping object that the function will use to create the device-independent bitmap. This parameter can be NULL.
If hSection is not NULL, it must be a handle to a file mapping object created by calling theCreateFileMapping function with the PAGE_READWRITE or PAGE_WRITECOPY flag. Read-only DIB sections are not supported. Handles created by other means will cause CreateDIBSection to fail.

If hSection is not NULL, the CreateDIBSection function locates the bitmap's bit values at offset dwOffset in the file mapping object referred to by hSection. An application can later retrieve the hSection handle by calling the GetObject function with theHBITMAP returned by CreateDIBSection.

If hSection is NULL, the system allocates memory for the device-independent bitmap. In this case, the CreateDIBSection function ignores the dwOffset parameter. An application cannot later obtain a handle to this memory. The dshSection member of the DIBSECTION structure filled in by calling the GetObject function will be NULL.

dwOffset
Specifies the offset from the beginning of the file mapping object referenced by hSection where storage for the bitmap's bit values is to begin. This value is ignored if hSection is NULL. The bitmap's bit values are aligned on doubleword boundaries, so dwOffset must be a multiple of the size of a DWORD.
Return Values
If the function succeeds, the return value is a handle to the newly created device-independent bitmap, and *ppvBits points to the bitmap's bit values.

If the function fails, the return value is NULL, and *ppvBits is NULL.

Windows NT: To get extended error information, callGetLastError.

Remarks
As noted above, if hSection is NULL, the system allocates memory for the device-independent bitmap. The system closes the handle to that memory when you later delete the device-independent bitmap by calling the DeleteObject function. If hSection is not NULL, you must close the hSection memory handle yourself after calling DeleteObject to delete the bitmap.

-------------
CreateFileMapping
The CreateFileMapping function creates a named or unnamed file-mapping object for the specified file.

HANDLE CreateFileMapping(
HANDLE hFile, // handle to file to map
LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
// optional security attributes
DWORD flProtect, // protection for mapping object
DWORD dwMaximumSizeHigh, // high-order 32 bits of object size
DWORD dwMaximumSizeLow, // low-order 32 bits of object size
LPCTSTR lpName // name of file-mapping object
);

Parameters
hFile
Handle to the file from which to create a mapping object. The file must be opened with an access mode compatible with the protection flags specified by the flProtect parameter. It is recommended, though not required, that files you intend to map be opened for exclusive access.
If hFile is (HANDLE)0xFFFFFFFF, the calling process must also specify a mapping object size in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. The function creates a file-mapping object of the specified size backed by the operating-system paging file rather than by a named file in the file system. The file-mapping object can be shared through duplication, through inheritance, or by name.

lpFileMappingAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpFileMappingAttributes is NULL, the handle cannot be inherited.


Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new file-mapping object. If lpFileMappingAttributes is NULL, the file-mapping object gets a default security descriptor.

flProtect
Specifies the protection desired for the file view, when the file is mapped. This parameter can be one of the following values: Value Description
PAGE_READONLY Gives read-only access to the committed region of pages. An attempt to write to or execute the committed region results in an access violation. The file specified by the hFile parameter must have been created with GENERIC_READ access.
PAGE_READWRITE Gives read-write access to the committed region of pages. The file specified by hFile must have been created with GENERIC_READ and GENERIC_WRITE access.
PAGE_WRITECOPY Gives copy on write access to the committed region of pages. The files specified by the hFile parameter must have been created with GENERIC_READ and GENERIC_WRITE access.


In addition, an application can specify certain section attributes by combining (using the bitwise OR operator) one or more of the following section attribute values with one of the preceding page protection values: Value Description
SEC_COMMIT Allocates physical storage in memory or in the paging file on disk for all pages of a section. This is the default setting.
SEC_IMAGE The file specified for a section's file mapping is an executable image file. Because the mapping information and file protection are taken from the image file, no other attributes are valid with SEC_IMAGE.
SEC_NOCACHE All pages of a section are to be set as non-cacheable. This attribute is intended for architectures requiring various locking structures to be in memory that is never fetched into the processor's. On 80x86 and MIPS machines, using the cache for these structures only slows down the performance as the hardware keeps the caches coherent. Some device drivers require noncached data so that programs can write through to the physical memory. SEC_NOCACHE requires either the SEC_RESERVE or SEC_COMMIT to also be set.
SEC_RESERVE Reserves all pages of a section without allocating physical storage. The reserved range of pages cannot be used by any other allocation operations until it is released. Reserved pages can be committed in subsequent calls to the VirtualAlloc function. This attribute is valid only if the hFile parameter is (HANDLE)0xFFFFFFFF; that is, a file mapping object backed by the operating sytem paging file.



dwMaximumSizeHigh
Specifies the high-order 32 bits of the maximum size of the file-mapping object.
dwMaximumSizeLow
Specifies the low-order 32 bits of the maximum size of the file-mapping object. If this parameter and dwMaximumSizeHig are zero, the maximum size of the file-mapping object is equal to the current size of the file identified by hFile.
lpName
Pointer to a null-terminated string specifying the name of the mapping object. The name can contain any character except the backslash character (\).
If this parameter matches the name of an existing named mapping object, the function requests access to the mapping object with the protection specified by flProtect.

If this parameter is NULL, the mapping object is created without a name.

If lpName matches the name of an existing event, semaphore, mutex, waitable timer, or job, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.

Return Values
If the function succeeds, the return value is a handle to the file-mapping object. If the object existed before the function call, the function returns a handle to the existing object (with its current size, not the specified size) and GetLastError returns ERROR_ALREADY_EXISTS.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.




[解决办法]
You can supply a handle to a file mapping object that the function will use to create the bitmap when not enough memory, or you can let the system allocate the memory for the bitmap when enough memory.

读书人网 >VB

热点推荐