bmp to jpg(32位bmp也可处理)

屏幕截图之后bmp 为4M 虽然本程序只是单图片传输 不过这个体积还是不能忍受的 

baidu google一下bmp to jpg 的文章 

找到

利用IJG JPEG Library压缩图像为jpg格式

这篇文章 不过文章中代码只能转换8位bmp和24位bmp

但是截屏bmp为32bit 所以我对其进行了转换 代码如下

 

换换后体积为100K左右 满足要求

 

 

/* * (8bit 24bit 32 bit )bmp to jmp * blog.csdn.net/farcall */ #include "stdafx.h" #include <windows.h> #include <stdlib.h> #include <Commdlg.h> #include <Shellapi.h> #include "string.h" extern "C" { #include "libjpeg/jpeglib.h" } #include <pshpack2.h> #pragma comment(lib, "libjpeg/libjpeg.lib") #pragma comment(lib,"Shell32.lib") //将内存中位图写为Bmp格式 //szFile为目标地址 void SaveBitmap(char *szFilename,HBITMAP hBitmap) { HDC hdc=NULL; FILE* fp=NULL; LPVOID pBuf=NULL; BITMAPINFO bmpInfo; BITMAPFILEHEADER bmpFileHeader; do{ hdc=GetDC(NULL); ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); GetDIBits(hdc,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS); if(bmpInfo.bmiHeader.biSizeImage<=0) bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8; if((pBuf=malloc(bmpInfo.bmiHeader.biSizeImage))==NULL) { // MessageBox(NULL,_T("Unable to Allocate Bitmap Memory"),_T("Error"),MB_OK|MB_ICONERROR); break; } bmpInfo.bmiHeader.biCompression=BI_RGB; GetDIBits(hdc,hBitmap,0,bmpInfo.bmiHeader.biHeight,pBuf,&bmpInfo,DIB_RGB_COLORS); if((fp=fopen(szFilename,"wb"))==NULL) { // MessageBox(NULL,_T("Unable to Create Bitmap File"),_T("Error"),MB_OK|MB_ICONERROR); break; } bmpFileHeader.bfReserved1=0; bmpFileHeader.bfReserved2=0; bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage; bmpFileHeader.bfType='MB'; bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp); fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp); fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp); }while(false); if(hdc) ReleaseDC(NULL,hdc); if(pBuf) free(pBuf); if(fp) fclose(fp); } //截图代码 void Screenshots() { HBITMAP hDesktopCompatibleBitmap=NULL; HDC hDesktopCompatibleDC=NULL; HDC hDesktopDC=NULL; HWND hDesktopWnd=NULL; RECT clientRect = {0,0,0,0}; NOTIFYICONDATA nid; bool bMinimized=false; int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; OPENFILENAME ofn; char szFileName[512]; DWORD nCount = GetTickCount(); strcpy(szFileName,"j://ScreenShot.bmp"); // sprintf(szFileName,"%s%d.bmp",szFileName,nCount); ZeroMemory(&ofn,sizeof(ofn)); ofn.lStructSize=sizeof(OPENFILENAME); ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST; ofn.lpstrFilter="Bitmap Files (*.bmp)/0*.bmp/0"; ofn.lpstrDefExt="bmp"; ofn.lpstrFile=szFileName; ofn.nMaxFile=512; SetCursor(LoadCursor(NULL,IDC_WAIT)); int nWidth=GetSystemMetrics(SM_CXSCREEN); int nHeight=GetSystemMetrics(SM_CYSCREEN); hDesktopDC = CreateDC("DISPLAY", NULL, NULL, NULL); HDC hBmpFileDC=CreateCompatibleDC(hDesktopDC); HBITMAP hBmpFileBitmap=CreateCompatibleBitmap(hDesktopDC,nWidth,nHeight); HBITMAP hOldBitmap = (HBITMAP) SelectObject(hBmpFileDC,hBmpFileBitmap); BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hDesktopDC,0,0,SRCCOPY|CAPTUREBLT); SelectObject(hBmpFileDC,hOldBitmap); SaveBitmap(ofn.lpstrFile,hBmpFileBitmap); DeleteDC(hBmpFileDC); DeleteObject(hBmpFileBitmap); SetCursor(LoadCursor(NULL,IDC_ARROW)); } /************************************************************************** 压缩图像到jpeg格式,如果压缩前的图像不是24位图,则强行转换为24位图后压缩 **************************************************************************/ void bmptojpg24(const char *strSourceFileName, const char *strDestFileName) { BITMAPFILEHEADER bfh; // bmp文件头 BITMAPINFOHEADER bih; // bmp头信息 RGBQUAD rq[256]; // 调色板 int i=0; BYTE *data= NULL;//new BYTE[bih.biWidth*bih.biHeight]; BYTE *pData24 = NULL;//new BYTE[bih.biWidth*bih.biHeight]; int nComponent = 0; // 打开图像文件 FILE *f = fopen(strSourceFileName,"rb"); if (f==NULL) { printf("Open file error!/n"); return; } // 读取文件头 fread(&bfh,sizeof(bfh),1,f); // 读取图像信息 fread(&bih,sizeof(bih),1,f); // 为了简单,在本例中,只演示8位索引图像 switch (bih.biBitCount) { case 8: if (bfh.bfOffBits-1024<54) { fclose(f); return; } data= new BYTE[bih.biWidth*bih.biHeight]; pData24 = new BYTE[bih.biWidth*bih.biHeight*3]; // 定位调色板,并读取调色板 fseek(f,bfh.bfOffBits-1024,SEEK_SET); fread(rq,sizeof(RGBQUAD),256,f); // 读取位图 fread(data,bih.biWidth*bih.biHeight,1,f); fclose(f); nComponent = 3; for (i=0;i<bih.biWidth * bih.biHeight ;i++) { pData24[i*3] = rq[data[i]].rgbRed; pData24[i*3+1] = rq[data[i]].rgbGreen; pData24[i*3+2] = rq[data[i]].rgbBlue; } break; case 24: { data= new BYTE[bih.biWidth*bih.biHeight*3]; pData24 = new BYTE[bih.biWidth*bih.biHeight*3]; fseek(f,bfh.bfOffBits,SEEK_SET); fread(data,bih.biWidth*bih.biHeight*3,1,f); fclose(f); for (i = 0;i<bih.biWidth*bih.biHeight;i++) { pData24[i*3] = data[i*3+2]; pData24[i*3+1] = data[i*3+1]; pData24[i*3+2] = data[i*3]; } nComponent = 3; break; } case 32: { data= new BYTE[bih.biWidth*bih.biHeight*4]; pData24 = new BYTE[bih.biWidth*bih.biHeight*4]; fseek(f,bfh.bfOffBits,SEEK_SET); fread(data,bih.biWidth*bih.biHeight*4,1,f); fclose(f); for (int i=0,int j=0;j< bih.biWidth*bih.biHeight*4 ;i+=3,j+=4) { *(pData24+i)=*(data+j+2); *(pData24+i+1)=*(data+j+1); *(pData24+i+2)=*(data+j); } nComponent = 3; break; } default: fclose(f); return; } // 以上图像读取完毕 struct jpeg_compress_struct jcs; struct jpeg_error_mgr jem; jcs.err = jpeg_std_error(&jem); jpeg_create_compress(&jcs); f=fopen(strDestFileName,"wb"); if (f==NULL) { delete [] data; //delete [] pDataConv; return; } jpeg_stdio_dest(&jcs, f); jcs.image_width = bih.biWidth; // 为图的宽和高,单位为像素 jcs.image_height = bih.biHeight; jcs.input_components = nComponent; // 1,表示灰度图, 如果是彩色位图,则为3 if (nComponent==1) jcs.in_color_space = JCS_GRAYSCALE; //JCS_GRAYSCALE表示灰度图,JCS_RGB表示彩色图像 else jcs.in_color_space = JCS_RGB; jpeg_set_defaults(&jcs); jpeg_set_quality (&jcs, 60, true); jpeg_start_compress(&jcs, TRUE); JSAMPROW row_pointer[1]; // 一行位图 int row_stride; // 每一行的字节数 row_stride = jcs.image_width*nComponent; // 如果不是索引图,此处需要乘以3 // 对每一行进行压缩 while (jcs.next_scanline < jcs.image_height) { row_pointer[0] = & pData24[(jcs.image_height-jcs.next_scanline-1) * row_stride]; jpeg_write_scanlines(&jcs, row_pointer, 1); } jpeg_finish_compress(&jcs); jpeg_destroy_compress(&jcs); fclose(f); delete [] data; delete [] pData24; } int main(int argc, char* argv[]) { Screenshots(); bmptojpg24("j://ScreenShot.bmp","j://xx.jpg"); return 0; }  

 

你可能感兴趣的:(null,delete,byte,FP,Components,colors)