bmp格式转换PNG格式 c语言或c++编程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bmp格式转换PNG格式 c语言或c++编程相关的知识,希望对你有一定的参考价值。
求程序,需要将BMP格式图片转换为PNG图片格式,需要c语言或c++编程程序,高手请赐教。
BMP是最简单的图形存储格式,在c++里有朋友封装了一个类CDib.只要把图片使用附件中编辑--粘贴来源找到图画打开另存为选择你想要的格式保存就可以了。也可以右键点击选择打开方式使用图画打开相同的方法。另外photoshop 和office2003的picture manage也有这个功能。
Private Sub mnuconvertBMPtoJPG_Click()
Dim tmpimage As imgdes ' Image descriptors
Dim tmp2image As imgdes
Dim rcode As Long
Dim quality As Long
Dim vbitcount As Long
Dim bdat As BITMAPINFOHEADER ' Reserve space for BMP struct
Dim bmp_fname As String
Dim jpg_fname As String
bmp_fname = "test.bmp"
jpg_fname = "test.jpg"
quality = 75
' Get info on the file we're to load
rcode = bmpinfo(bmp_fname, bdat)
If (rcode <> NO_ERROR) Then
MsgBox "Cannot find file", 0, "Error encountered!"
Exit Sub
End If
vbitcount = bdat.biBitCount
If (vbitcount >= 16) Then ' 16-, 24-, or 32-bit image is loaded into 24-bit buffer
vbitcount = 24
End If
' Allocate space for an image
rcode = allocimage(tmpimage, bdat.biWidth, bdat.biHeight, vbitcount)
If (rcode <> NO_ERROR) Then
MsgBox "Not enough memory", 0, "Error encountered!"
Exit Sub
End If
' Load image
rcode = loadbmp(bmp_fname, tmpimage)
If (rcode <> NO_ERROR) Then
freeimage tmpimage ' Free image on error
MsgBox "Cannot load file", 0, "Error encountered!"
Exit Sub
End If
If (vbitcount = 1) Then ' If we loaded a 1-bit image, convert to 8-bit grayscale
' because jpeg only supports 8-bit grayscale or 24-bit color images
rcode = allocimage(tmp2image, bdat.biWidth, bdat.biHeight, 8)
If (rcode = NO_ERROR) Then
rcode = convert1bitto8bit(tmpimage, tmp2image)
freeimage tmpimage ' Replace 1-bit image with grayscale image
copyimgdes tmp2image, tmpimage
End If
End If
' Save image
rcode = savejpg(jpg_fname, tmpimage, quality)
freeimage tmpimage
End Sub
........... Add these defines and declarations to your Global module ...........
' Image descriptor
Type imgdes
ibuff As Long
stx As Long
sty As Long
endx As Long
endy As Long
buffwidth As Long
palette As Long
colors As Long
imgtype As Long
bmh As Long
hBitmap As Long
End Type
Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Declare Function bmpinfo Lib "VIC32.DLL" (ByVal Fname As String, bdat As BITMAPINFOHEADER) As Long
Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long
Declare Function loadbmp Lib "VIC32.DLL" (ByVal Fname As String, desimg As imgdes) As Long
Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function convert1bitto8bit Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes) As Long
Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
Declare Function savejpg Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal quality As Long) As Long
《图像处理----做一个自己的photoshop》
大部分都是源码,其中有bmp<-->jgep<-->GIF的代码. 参考技术A
1、如何从BMP图片读取每个像素,然后根据PNG图片的格式以及RGB色彩算法,将BMP像素转换成png图片,最后根据PNG格式输出到文件。
例程:
FILE *fp = NULL;char buf[PNG_BYTES_TO_CHECK];
LPSTR lpszFileName = lpszPathName;
if(!lpszFileName) return false;
//打开PNG文件
if(NULL == (fp = fopen(lpszFileName,"rb")))
delete []lpszFileName;
lpszFileName = NULL;
return false;
//验证是否为PNG文件
if(fread(buf,1,PNG_BYTES_TO_CHECK,fp) != PNG_BYTES_TO_CHECK)
fclose(fp);
delete []lpszFileName;
lpszFileName = NULL;
return false;
if( 0 != png_sig_cmp((unsigned char*)buf,(png_size_t)0,PNG_BYTES_TO_CHECK))
fclose(fp);
delete []lpszFileName;
lpszFileName = NULL;
return false;
//以下代码读取PNG文件
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
unsigned int sig_read = PNG_BYTES_TO_CHECK;
int color_type = 0,interlace_type = 0;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if( NULL == png_ptr)
fclose(fp);
delete []lpszFileName;
lpszFileName = NULL;
return false;
info_ptr = png_create_info_struct(png_ptr);
if(NULL == info_ptr)
fclose(fp);
png_destroy_read_struct(&png_ptr,&info_ptr,png_infopp_NULL);
delete []lpszFileName;
lpszFileName = NULL;
return false;
png_init_io(png_ptr,fp);
png_set_sig_bytes(png_ptr,sig_read);
png_read_info(png_ptr,info_ptr);
if(info_ptr->bit_depth == 16)png_set_strip_16(png_ptr);
if(info_ptr->color_type == PNG_COLOR_TYPE_GRAY && info_ptr->pixel_depth < 8)
png_set_gray_1_2_4_to_8(png_ptr);
switch (info_ptr->pixel_depth)
case 32:
if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)png_set_bgr(png_ptr);
png_set_invert_alpha(png_ptr);
//当为32位图像是应该如何处理,例如
//pDib->CreateDIB(info_ptr->width, info_ptr->height,32);
break;
case 24:
png_color_16 my_background=0,255,255,255,0;
png_set_background(png_ptr,&my_background,
PNG_BACKGROUND_GAMMA_SCREEN,0,1.0);
if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)png_set_bgr(png_ptr);
//当为32位图像是应该如何处理,例如
//pDib->CreateDIB(info_ptr->width, info_ptr->height,24);
break;
case 8:
case 4:
//当为32位图像是应该如何处理,例如
//if(info_ptr->num_palette>0)
//
// DWORD *pRQ=new DWORD[info_ptr->num_palette];
// for(int i=0;i<info_ptr->num_palette;i++)
//
// pRQ[i]=RGB(info_ptr->palette[i].red,info_ptr->palette[i].green,info_ptr->palette[i].blue);
//
// pDib->CreateDIB(info_ptr->width, info_ptr->height,info_ptr->pixel_depth,pRQ,info_ptr->num_palette);
// delete[] pRQ;
//
break;
default:
return false;
png_bytepp prow_pointers = new png_bytep[info_ptr->height];
UINT nRowBytes = png_get_rowbytes(png_ptr,info_ptr);
UINT row;
for(row=0;row<info_ptr->height;row++)
prow_pointers[row]=new BYTE[nRowBytes];
png_read_image(png_ptr,prow_pointers);
png_read_end(png_ptr,info_ptr);
//拷贝转换后的图像数据
for(row=0;row<info_ptr->height;row++)
memcpy(pDib->m_lpImage+(info_ptr->height-row-1)*pDib->m_nByteWidth,
prow_pointers[row],pDib->m_nByteWidth);
2、更简单的方法:用windows自带的图片和传真查看器打开要转换的图片,点击右下角的保存按钮,弹出保存对话框,选择要保存的类型。笔者经实验可转换的类型为gif,bmp,png,jpg.最大的优点是方便且不失。
参考技术B 查找关于图像的函数。VB.NET的话是IMAGE类下面有相关的类来实现。
HEIC文件怎么打开,一键批量将HEIC图片转换JPG/PNG/BMP
heic图片是无法使用windows图片产查看器或者其他图片查看工具打开,因为ISO系统在更新到ios11版本后,iphone手机拍摄的文件格式为HEIC格式图片, 这种格式占用体积小,对于非苹果用户很不友好,所以在win7和win10系统下需要使用 “HEIC图片转换精灵” 将heic图片转成普通图片,将heic转成无损jpg,png,bmp 等常见格式,解决不同系统无法打开heic图片的限制.
支持系统: win7/win10
软件获取地址:www.heicghost.com
1.第一步打开安装的软件,设置输出格式,点击下拉列表选取转换后的格式,默认jpg,支持输出jpg,png,bmp ,然后点击添加文件夹按钮,批量导入文件,导入成功后文件列表显示导入的文件.
2.第二步,列表中添加的文件默认是勾选的,只需一键点击开始转换,等待数秒钟,列表文件状态栏显示绿色的勾,代表文件转换完成,点击开目录,转换后jpg/pmg/bmp图片就在打开的目录下.
以上是关于bmp格式转换PNG格式 c语言或c++编程的主要内容,如果未能解决你的问题,请参考以下文章
如何将png格式、jpg格式的图片转成24bit的bmp图片。