TC如何载入BMP或jpg的图片,希望能给出代码~~~

作者&投稿:羿闵 (若有异议请与网页底部的电邮联系)
MFC中如何载入bmp和jpeg格式的图片,并作为文件背景~

PSP是一台索尼推出的掌机,它可以通过安装模拟器来运行以上游戏。

下面是源代码:
添加picturebox,commandbutton

Option Explicit
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type

Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type

Private Type EncoderParameter
GUID As GUID
NumberOfValues As Long
type As Long
Value As Long
End Type

Private Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type

Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hpal As Long, Bitmap As Long) As Long
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal filename As Long, clsidEncoder As GUID, encoderParams As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As Long, Bitmap As Long) As Long

Private Sub Command1_Click()
Dim ret As Boolean

Picture1.Picture = LoadPicture("C:\a.bmp") '打开要压缩的图片

ret = PictureBoxSaveJPG(Picture1, "C:\b.jpg") '保存压缩后的图片
If ret = False Then
MsgBox "保存失败"
End If
End Sub

Private Function PictureBoxSaveJPG(ByVal pict As StdPicture, ByVal filename As String, Optional ByVal quality As Byte = 80) As Boolean
Dim tSI As GdiplusStartupInput
Dim lRes As Long
Dim lGDIP As Long
Dim lBitmap As Long

'初始化 GDI+
tSI.GdiplusVersion = 1
lRes = GdiplusStartup(lGDIP, tSI, 0)

If lRes = 0 Then
'从句柄创建 GDI+ 图像
lRes = GdipCreateBitmapFromHBITMAP(pict.Handle, 0, lBitmap)

If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters

'初始化解码器的GUID标识
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder

'设置解码器参数
tParams.Count = 1
With tParams.Parameter ' Quality
'得到Quality参数的GUID标识
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
.NumberOfValues = 1
.type = 4
.Value = VarPtr(quality)
End With

'保存图像
lRes = GdipSaveImageToFile(lBitmap, StrPtr(filename), tJpgEncoder, tParams)

'销毁GDI+图像
GdipDisposeImage lBitmap
End If

'销毁 GDI+
GdiplusShutdown lGDIP
End If

If lRes Then
PictureBoxSaveJPG = False
Else
PictureBoxSaveJPG = True
End If
End Function


http://tieba.baidu.com/f?ct=335675392&tn=baiduPostBrowser&sc=3183913151&z=314561243&pn=0&rn=50&lm=0&word=vb#3183913151 http://tieba.baidu.com/f?kz=333370629http://download.csdn.net/source/1041194

int write_jpeg(char *filename,unsigned char *buf,int quality,int width, int height, int gray)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *fp;
int i;
unsigned char *line;
int line_length;
if (NULL == (fp = fopen(filename,"w")))
{
printf("can't open %s\n",filename);
return -1;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, fp);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = gray ? 1: 3;
cinfo.in_color_space = gray ? JCS_GRAYSCALE: JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
line_length = gray ? width : width * 3;
for (i = 0, line = buf; i < height; i++, line += line_length)
jpeg_write_scanlines(&cinfo, &line, 1);
jpeg_finish_compress(&(cinfo));
jpeg_destroy_compress(&(cinfo));
fclose(fp);
return 0;
}
///////////////////////////////////////////////////////////////

struct my_error_mgr
{
struct jpeg_error_mgr pub; /* "public" fields */

jmp_buf setjmp_buffer; /* for return to caller */
};

typedef struct my_error_mgr * my_error_ptr;

void my_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
my_error_ptr myerr = (my_error_ptr) cinfo->err;

/* Always display the message. */
/* We could postpone this until after returning, if we chose. */
(*cinfo->err->output_message) (cinfo);

/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}

int read_JPEG_file (char * filename)
{
/* This struct contains the JPEG decompression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
*/
struct jpeg_decompress_struct cinfo;
/* We use our private extension JPEG error handler.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
struct my_error_mgr jerr;
/* More stuff */
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */

if ((infile = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "can't open %s\n", filename);
return 0;
}

/* Step 1: allocate and initialize JPEG decompression object */
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;

if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return 0;
}
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&cinfo, infile);
/* Step 3: read file parameters with jpeg_read_header() */
jpeg_read_header(&cinfo, TRUE);//ignore the return value
/* Step 5: Start decompressor */
jpeg_start_decompress(&cinfo);//ignore the return value

/* JSAMPLEs per row in output buffer */
row_stride = cinfo.output_width * cinfo.output_components;
/* Make a one-row-high sample array that will go away when done with image */
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);

BITMAPFILEHEADER bmphead;
bmphead.bfType = 0x4D42;
bmphead.bfsize_part1 = 0x36;
bmphead.bfsize_part2 = 0x10;
bmphead.bfsize_part3 = 0x0E;
bmphead.bfsize_part4 = 0x00;

bmphead.reserved_part1 = 0x00;
bmphead.reserved_part2 = 0x00;
bmphead.reserved_part3 = 0x00;
bmphead.reserved_part4 = 0x00;

bmphead.bfOffBits_part1 = 0x36;
bmphead.bfOffBits_part2 = 0x00;
bmphead.bfOffBits_part3 = 0x00;
bmphead.bfOffBits_part4 = 0x00;
//char bmphead[14] = {0x4D,0x42,0x36,x010,0x0E,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00};
//printf("%d\n",sizeof(bmphead));

BITMAPINFOHEADER inforhead;
inforhead.bisize = 40;
inforhead.biwidth = 640;
inforhead.biheight = 480;
inforhead.biPlants = 1;
inforhead.bibitcount = 24;
inforhead.BICOMPRESSION = 0;
inforhead.BISIZEIMAGE = 0;//640 * 480 * 3;
//inforhead.BISIZEIMAGE_part1 = 0x00;

//inforhead.bixpelspermeter = 120;
//inforhead.biypelspermeter = 120;
inforhead.bixpelspermeter_part1 = 0x74;
inforhead.bixpelspermeter_part2 = 0x12;
inforhead.bixpelspermeter_part3 = 0x00;
inforhead.bixpelspermeter_part4 = 0x00;

inforhead.biypelspermeter_part1 = 0x74;
inforhead.biypelspermeter_part2 = 0x12;
inforhead.biypelspermeter_part3 = 0x00;
inforhead.biypelspermeter_part4 = 0x00;

inforhead.biclrused = 0;
inforhead.biclrimportant = 0;

//printf("%d\n",sizeof(inforhead));

FILE *fp;
if (NULL == (fp = fopen("/home/jeffrey/1.bmp","wb")))
{
return -1;
}
fwrite(&bmphead,14,1,fp);
fwrite(&inforhead,40,1,fp);

printf("output_width : %d\n",cinfo.output_width);
printf("output_components : %d\n",cinfo.output_components);
printf("output_height : %d\n",cinfo.output_height);

while (cinfo.output_scanline < cinfo.output_height)
{

jpeg_read_scanlines(&cinfo, buffer, 1);

fwrite(*buffer,row_stride,1,fp);

}

fclose(fp);

jpeg_finish_decompress(&cinfo);

jpeg_destroy_decompress(&cinfo);

fclose(infile);

return 1;
}

打开windows自带的软件。
将你的bmp的图片打开,另存为.jpg文件即可。

也可以用PhotoShop打开,找到jpg格式保存。
.
希望能够帮助你 ^_^ 也希望能够选为最佳答案!


C语言调用图片
VC里面可以直接调用bmp格式的文件 用CBitmap类就可以了 \/\/CBitmap对象 CBitmap bitMap;\/\/设备环境类对象 CDC dcMemory;\/\/加载资源位图 bitMap.LoadBitmap(IDB_BITMAP1);\/\/bitMap.LoadBitmap("xxx.bmp");\/\/创建内存设备环境 dcMemory.CreateCompatibleDC(pDC);\/\/把位图选进内存设备环境,并保存旧...

怎么用C语言中的fopen函数打开bmp格式的图像文件
1.图片也是属于文件类型的一种,图片属于二进制文件。使用fopen函数的二进制模式“rb”就可以打开。2.例程:include <stdlib.h> #include <stdio.h> int main () { FILE * fpPhoto, * fpText, * fpTarget ; int iRead ; char szBuf[100] ; printf ("请输入第...

高手指点:用C语言编写一个读写“*.BMP”文件信息的程序
高手指点:用C语言编写一个读写“*.BMP”文件信息的程序 是读写BMP文件的具体信息(包括文件头和每个像素的RGB值),如被被采纳,必重谢!!!... 是读写BMP文件的具体信息(包括文件头和每个像素的RGB值),如被被采纳,必重谢!!! 展开  我来答 ...

请问一下,c语言怎么实现载入图片,是黑窗口的,
用 GetConsoleWindow 获得控制台的窗口句柄,然后用 GetDC 获得 GDI 设备上下文 之后就可以用上下文和绘图函数画 BMP 了

在vs2008用c语言,如何在窗体里插入图片。
第一步:选好.bmp的类型的图片。在资源视图中导入。随便复制到.res文件夹下。第二步。在对话框上添加picture控件,调整到大小。右击属性选择Type选择Bitmap.Image下拉组合框选择你导入的图片。运行时即可显示图片。如果要设置和窗体一样的大小SetWindowPos就可以了。查查这个函数的作用 ...

谁有VC++项目 关于加载图像(bmp,jpg等都行)的例子,传我一份,939504672@...
CBitmap bmp;bmp.Attach((HBITMAP)phandle);BITMAP tbmp;bmp.GetBitmap(&tbmp);\/\/创建数组 byte *pbyBuf = new byte[tbmp. bmWidthBytes*tbmp.bmHeight*];\/\/ 定义位图信息 BITMAPINFO bi;bi.bmiHeader.biSize = sizeof(bi.bmiHeader);bi.bmiHeader.biWidth = tbmp.bmWidth;bi.bmiHeader....

C语言 读取黑白BMP图
1.黑白BMP文件图的像素大多是从62字节(从0数起)开始。具体从哪一字节开始,决定于文件中第10字节(从0数起)开始处4字节的整形数是多少.因为文件头后面不是紧接着像素值.2.黑白BMP图的一个像素用一个bit表示。一般0表示背景(缺省为白色),1表示前景色(缺省为黑色)。即一个字节代表相邻的8个...

怎么用C语言中的fopen函数打开bmp格式的图像文件
fopen看到的文件都是字节流。你可以和打开任何文件一样打开bmp,但你独到的是字节流。再看看别人怎么说的。

如何使用C++的读取文件流读取一个24位真彩色的BMP文件到一个数组当中...
byte[] bmps = System.IO.File.ReadAllBytes("a.bmp");2.所谓路径。路径是文件系统的一个概念,对于一个Windows文件系统来说,路径是个树形结构,每棵树都有一个根节点。把硬盘分成数个分区(C:,D:,E:……),每个分区又可以建立数个文件夹,在文件夹下面可以建立其他文件夹或者文件。其中每个...

c\/c++ winsock 如何发送和接收图片bmp格式文件
我以C语言的办法来简单描述下 客户端:1。发送连接申请到服务端 2。使用fopen打开要发送的BMP文件 3。使用fread函数读取数据存入变量中 4。使用函数send来将变量中内容发送到服务端 服务端:1。接受客户端连接申请 2。使用fopen函数创建一个BMP文件 3。使用函数recv来接收传送过来的数据存入变量中 4。

金阊区19591238943: TC如何载入BMP或jpg的图片,希望能给出代码~~~ -
钮钱维春: int write_jpeg(char *filename,unsigned char *buf,int quality,int width, int height, int gray) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *fp; int i; unsigned char *line; int line_length; if (NULL == (fp = fopen(filename,"w"))) { printf...

金阊区19591238943: MFC中如何载入bmp和jpeg格式的图片,并作为文件背景 -
钮钱维春: 通过资源加载BMP位图的方法: CDC m_MemDc;//定义一个DC CBitmap m_BkBmp;//定义个位图对象 BITMAP m_BmpInfo;//定义一个位图信息结构体 CDC *pDc = GetDC();//定义一个DC指针,并指向屏幕DC/*首先在资源里添加位图,...

金阊区19591238943: C++怎么加载一张jpg图片并且把它作为对话框的背景,求大神帮忙呀...谢谢 -
钮钱维春: 在win32 C++中,默认只支持位图.而其他的高级图片,例如png或者jpeg 都需要自己先找一个解码器,解码器先负责将png,jpeg什么的转化成 bmp bmp的格式非常好解析,简单的数据头,后面就是 RGB数据了 可以使用 GDI 或者 GDI+ 或者 DirectX 2D来渲染图片.后着比前两者速度都快得多. MFC中,默认是在OnPaint函数中进行处理.

金阊区19591238943: c语言怎么添加图片或音乐?给个例子也可以的 -
钮钱维春: JPG是压缩格式,我不会,我只会BMP的, 之前刚好完成课程设计做了一份 #include "stdlib.h" #include "graphics.h" #include "stdio.h"#define WIDTH 120 #define HEIGHT 120 用一个二维数组保存的图片,这里图片长高必须是4的倍数...

金阊区19591238943: MFC怎么将jpg格式图片插入到资源中? -
钮钱维春: 两种方法:1. 当做二进制数据.这样可以将任何文件加入资源,但需要自己解码显示(或者GDI+)2. 新建bitmap位图,然后将jpg使用图片编辑器打开,全选,复制,粘贴到bitmap

金阊区19591238943: TEMS如何导入BMP格式的图 -
钮钱维春: 把室分图纸弄成bmp图片格式.进入tems,在map窗口,第三个按钮(就是position map),打开你的bmp图片,他就自动在图片目录生成一tab文件.还是tems的map窗口,第一个按钮geost manager,新建一geost,打开你的tab文件,然后另存为geost,就ok了 入室内地图:点击Open map选择所要导入的Bmp图(此图的命名务必不能是中文)点击确认

金阊区19591238943: C++ 怎么加载本地的BMP图片啊 不是MFC的 这个图片的路径是E:\赛马\Main\loading.BMP 要写代码的,不带控件 -
钮钱维春: 读入内存,你自己解析bmp文件的格式,然后按照自己的需要做各种效果变化,需要显示它的时候直接bitblt逐位传输显示即可.细节你可以参考windows程序设计,里面讲了位图的格式.

金阊区19591238943: 如何上传BMP类的相片
钮钱维春: 是格式不对吧 右键点击它,然后点"编辑" 在点在"文件"弹出一个下拉菜单,后点"另存为" 出来个对话框 "保存类型"里改图片扩展名该为:JPG或GIF的 然后上传

金阊区19591238943: 在PHOTO SHOP里怎么导入BMP和JPG格式的图片?
钮钱维春: 双击空白出就行,或者在新建里找导入就可以了!

金阊区19591238943: 在MFC当中,怎么动态加载图片(bmp、jpg、gif等),还有就是如果使用picture控件,代码该怎么写? -
钮钱维春: GDI好像只能显示BMP,如果硬要用GDI显示的话就要用流了,那个蛮麻烦的,其实用GDI+来显示的话,只用调个函数就行了,蛮方便的,关键是看编绎器支不支持,不支持还得装GDI+

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网