bmp格式转换PNG格式 c语言或c++编程
3个回答
展开全部
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的代码.
只要把图片使用附件中编辑--粘贴来源找到图画打开另存为选择你想要的格式保存就可以了。也可以右键点击选择打开方式使用图画打开相同的方法。另外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的代码.
展开全部
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.最大的优点是方便且不失。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
查找关于图像的函数。
VB.NET的话是IMAGE类下面有相关的类来实现。
VB.NET的话是IMAGE类下面有相关的类来实现。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询