代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
保存位图
作者:
Dezai.CN
/ 发布于
2012/2/13
/
811
<div> <span style="font-size: 13px;">static BOOL SaveBitmap (HBITMAP hBmp, const char* pszFile)</span> <span style="font-size: 13px;">{ </span> <span style="font-size: 13px;">BITMAP bmp; </span> <span style="font-size: 13px;">PBITMAPINFO pbmi; </span> <span style="font-size: 13px;">WORD cClrBits; /* </span><span style="font-size: 13px;">Retrieve the bitmap's color format, width, and height. */ </span> <span style="font-size: 13px;">if (!</span><span style="font-size: 13px;">GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)) </span> <span style="font-size: 13px;"> return </span><span style="font-size: 13px;">FALSE; </span> <span style="font-size: 13px;"> /* Convert the color format to a count of bits. */ </span> <span style="font-size: 13px;">cClrBits = </span><span style="font-size: 13px;">(WORD)(bmp.bmPlanes * bmp.bmBitsPixel); </span> <span style="font-size: 13px;">if (cClrBits == 1) </span> <span style="font-size: 13px;">cClrBits = 1; </span> <span style="font-size: 13px;">else if (cClrBits <= 4) </span> <span style="font-size: 13px;"> cClrBits = 4; </span> <span style="font-size: 13px;"> else if (cClrBits </span><span style="font-size: 13px;"><= 8) </span> <span style="font-size: 13px;">cClrBits = 8; </span> <span style="font-size: 13px;">else if (cClrBits <= 16) </span> <span style="font-size: 13px;">cClrBits = 16; </span> <span style="font-size: 13px;">else if </span><span style="font-size: 13px;">(cClrBits <= 24) </span> <span style="font-size: 13px;">cClrBits = 24; </span> <span style="font-size: 13px;"> else cClrBits = 32;</span> <span style="font-size: 13px;"> /* * Allocate </span><span style="font-size: 13px;">memory for the BITMAPINFO structure. (This structure *</span> <span style="font-size: 13px;">contains a BITMAPINFOHEADER structure and an array of</span> <span style="font-size: 13px;">RGBQUAD data * structures.) */ </span> <span style="font-size: 13px;">if (cClrBits < 24) </span> <span style="font-size: 13px;"> pbmi = </span><span style="font-size: 13px;">(PBITMAPINFO) GlobalAlloc(LPTR, </span><span style="font-size: 13px;">sizeof(BITMAPINFOHEADER) + </span><span style="font-size: 13px;">sizeof(RGBQUAD) * (1 << cClrBits)); </span> <span style="font-size: 13px;">/* * There is no </span><span style="font-size: 13px;">RGBQUAD array for the 24-bit-per-pixel format. */ </span> <span style="font-size: 13px;"> else </span> <span style="font-size: 13px;">pbmi </span><span style="font-size: 13px;">= (PBITMAPINFO) GlobalAlloc(LPTR, </span><span style="font-size: 13px;">sizeof(BITMAPINFOHEADER)); </span> <span style="font-size: 13px;">/* Initialize the fields in the </span><span style="font-size: 13px;">BITMAPINFO structure. */ </span> <span style="font-size: 13px;">pbmi->bmiHeader.biSize = sizeof</span><span style="font-size: 13px;">(BITMAPINFOHEADER); </span> <span style="font-size: 13px;">pbmi->bmiHeader.biWidth = </span><span style="font-size: 13px;">bmp.bmWidth; </span> <span style="font-size: 13px;">pbmi->bmiHeader.biHeight = bmp.bmHeight; </span> <span style="font-size: 13px;">pbmi->bmiHeader.biPlanes = bmp.bmPlanes; </span> <span style="font-size: 13px;">pbmi-</span><span style="font-size: 13px;">>bmiHeader.biBitCount = bmp.bmBitsPixel; </span> <span style="font-size: 13px;">if (cClrBits < 24) </span> <span style="font-size: 13px;">pbmi->bmiHeader.biClrUsed = (1 << cClrBits); </span> <span style="font-size: 13px;">else </span> <span style="font-size: 13px;">pbmi-</span><span style="font-size: 13px;">>bmiHeader.biClrUsed = 0; </span> <span style="font-size: 13px;">/* If the bitmap is not compressed, set </span><span style="font-size: 13px;">the BI_RGB flag. */ </span> <span style="font-size: 13px;">pbmi->bmiHeader.biCompression = BI_RGB; </span> <span style="font-size: 13px;">/* * Compute the number of bytes in the array of color * indices</span><span style="font-size: 13px;">and store the result in biSizeImage. */ </span> <span style="font-size: 13px;">pbmi-</span><span style="font-size: 13px;">>bmiHeader.biSizeImage = (pbmi->bmiHeader.biWidth + 7) /8 </span><span style="font-size: 13px;">* pbmi->bmiHeader.biHeight </span><span style="font-size: 13px;"> * cClrBits; </span> <span style="font-size: 13px;"> /* * Set biClrImportant to 0, indicating </span><span style="font-size: 13px;">that all of the * device colors are important. */ pbmi-</span><span style="font-size: 13px;">>bmiHeader.biClrImportant = 0;</span> <span style="font-size: 13px;"> { /* C sucks */ </span> <span style="font-size: 13px;">HANDLE hf; </span> <span style="font-size: 13px;"> /* file handle */ </span> <span style="font-size: 13px;">BITMAPFILEHEADER hdr; </span> <span style="font-size: 13px;"> /* bitmap file-</span><span style="font-size: 13px;">header */ </span> <span style="font-size: 13px;">PBITMAPINFOHEADER pbih; </span> <span style="font-size: 13px;">/* bitmap info-header </span><span style="font-size: 13px;">*/ </span> <span style="font-size: 13px;">LPBYTE lpBits; </span> <span style="font-size: 13px;"> /* memory pointer */ </span> <span style="font-size: 13px;">DWORD dwTotal; </span> <span style="font-size: 13px;">/* total count of bytes */ </span> <span style="font-size: 13px;">DWORD cb; </span> <span style="font-size: 13px;">/* </span><span style="font-size: 13px;">incremental count of bytes */ </span> <span style="font-size: 13px;">DWORD dwTmp; </span> <span style="font-size: 13px;">HDC hDC;</span> <span style="font-size: 13px;"> pbih = </span><span style="font-size: 13px;">(PBITMAPINFOHEADER) pbmi; </span> <span style="font-size: 13px;">lpBits = (LPBYTE) GlobalAlloc</span><span style="font-size: 13px;">(GMEM_FIXED, pbih->biSizeImage); </span> <span style="font-size: 13px;">if (!lpBits) </span> <span style="font-size: 13px;">return FALSE; </span> <span style="font-size: 13px;">/* * Retrieve the color table (RGBQUAD array) and the bits *</span><span style="font-size: 13px;">(array of palette indices) from the DIB. */ </span> <span style="font-size: 13px;">hDC = </span><span style="font-size: 13px;">CreateCompatibleDC(NULL); </span> <span style="font-size: 13px;">if (!GetDIBits(hDC, hBmp, 0, </span><span style="font-size: 13px;">(WORD) pbih->biHeight, lpBits, pbmi,</span><span style="font-size: 13px;">DIB_RGB_COLORS)) return FALSE; </span> <span style="font-size: 13px;">/* Create the .BMP file. */ </span> <span style="font-size: 13px;">hf = CreateFile (pszFile, GENERIC_READ | </span><span style="font-size: 13px;">GENERIC_WRITE, (DWORD) 0, </span> <span style="font-size: 13px;">(LPSECURITY_ATTRIBUTES) NULL, </span> <span style="font-size: 13px;">CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, </span> <span style="font-size: 13px;"> (HANDLE) NULL);</span> <span style="font-size: 13px;"> if (hf == </span><span style="font-size: 13px;">INVALID_HANDLE_VALUE) </span> <span style="font-size: 13px;">return FALSE; </span> <span style="font-size: 13px;"> hdr.bfType = </span><span style="font-size: 13px;">0x4d42; </span> <span style="font-size: 13px;">/* 0x42 = "B" 0x4d = "M" */ </span> <span style="font-size: 13px;"> /* Compute the size of the </span><span style="font-size: 13px;">entire file. */ </span> <span style="font-size: 13px;">hdr.bfSize = (DWORD) (sizeof</span><span style="font-size: 13px;">(BITMAPFILEHEADER) + pbih-</span><span style="font-size: 13px;">>biSize + pbih->biClrUsed * sizeof</span><span style="font-size: 13px;">(RGBQUAD) + pbih->biSizeImage); </span> <span style="font-size: 13px;">hdr.bfReserved1 = 0; </span> <span style="font-size: 13px;">hdr.bfReserved2 = 0; </span> <span style="font-size: 13px;">/* Compute the offset to the array of color </span><span style="font-size: 13px;">indices. */ </span> <span style="font-size: 13px;">hdr.bfOffBits = (DWORD) sizeof</span><span style="font-size: 13px;">(BITMAPFILEHEADER) + pbih-</span><span style="font-size: 13px;">>biSize + pbih->biClrUsed * sizeof</span><span style="font-size: 13px;">(RGBQUAD); </span> <span style="font-size: 13px;">/* Copy the BITMAPFILEHEADER into the .BMP </span><span style="font-size: 13px;">file. */ </span> <span style="font-size: 13px;">if (!WriteFile(hf, (LPVOID) &hdr, sizeof</span><span style="font-size: 13px;">(BITMAPFILEHEADER), (LPDWORD) &dwTmp,</span> <span style="font-size: 13px;">(LPOVERLAPPED) NULL)) </span> <span style="font-size: 13px;">return FALSE; </span> <span style="font-size: 13px;">/* Copy the </span><span style="font-size: 13px;">BITMAPINFOHEADER and RGBQUAD array into the file. */ </span> <span style="font-size: 13px;"> if </span><span style="font-size: 13px;">(!WriteFile(hf, (LPVOID) pbih, sizeof(BITMAPINFOHEADER) </span><span style="font-size: 13px;"> + pbih->biClrUsed * sizeof (RGBQUAD), </span><span style="font-size: 13px;">(LPDWORD) &dwTmp, (LPOVERLAPPED) NULL)) </span> <span style="font-size: 13px;">return </span><span style="font-size: 13px;">FALSE; </span> <span style="font-size: 13px;"> /* Copy the array of color indices into the .BMP file. */ </span> <span style="font-size: 13px;">dwTotal = cb = pbih->biSizeImage;</span> <span style="font-size: 13px;"> if (!WriteFile(hf, (LPSTR) </span><span style="font-size: 13px;">lpBits, (int) cb, (LPDWORD) &dwTotal,</span><span style="font-size: 13px;">(LPOVERLAPPED) NULL)) return FALSE;</span> <span style="font-size: 13px;"> /* Close the .BMP </span><span style="font-size: 13px;">file. */ </span> <span style="font-size: 13px;">if (!CloseHandle(hf)) </span> <span style="font-size: 13px;">return FALSE;</span> <span style="font-size: 13px;"> /* Free memory. */ </span> <span style="font-size: 13px;">GlobalFree((HGLOBAL)lpBits); </span> <span style="font-size: 13px;">GlobalFree((HGLOBAL)pbmi);</span> <span style="font-size: 13px;">DeleteDC(hDC); </span> <span style="font-size: 13px;">} </span> <span style="font-size: 13px;">/* C sucks */ </span> <span style="font-size: 13px;"> return TRUE; </span> <span style="font-size: 13px;">}</span> </div>
试试其它关键字
保存位图
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3