GDI+往jpg图片文件里面添加属性值

30 篇文章 1 订阅

最近有个项目需求:

1.如果图片是非jpg格式的如bmp格式的则将bmp格式的图片文件转为;

2.往jpeg图片中添加两个属性值用来以读取属性值进行图片的分类查找等操作;

所实现的功能简单,所以就用了c++的GDI+库。但是在实现的过程中确实遇到了不少奇怪的问题:

(1)首先是将所需要的头文件和lib库导入工程:

#include <windows.h>

#include <gdiplus.h>

#include <stdio.h>

using namespace Gdiplus;


和Gdiplus.lib 库


(2)下面是msdn上面的调用:


#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;


INT main()
{
   GdiplusStartupInput gdiplusStartupInput;
   ULONG_PTR gdiplusToken;
   GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);


   // Create an Image object based on a JPEG file.
   Image* image = new Image(L"FakePhoto.jpg");  //可以为文件的具体路径:C:\\FakePhoto.jpg


   // Set the image title.
   PropertyItem* propItem = new PropertyItem;
   CHAR newTitleValue[] = "Fake Photograph 2";


   propItem->id = PropertyTagImageTitle;
   propItem->length = 18;  //  includes null terminator
   propItem->type = PropertyTagTypeASCII;
   propItem->value = newTitleValue;


   image->SetPropertyItem(propItem);


   // Get and display the new image title.
   UINT size = image->GetPropertyItemSize(PropertyTagImageTitle);
   PropertyItem* title = (PropertyItem*)malloc(size);
   image->GetPropertyItem(PropertyTagImageTitle, size, title);
   printf("The image title is %s.\n", title->value);//这里只是获取的加载图片后在内存的值,并没有将添加的属性值添加到图片上面去


   free(title);
   delete propItem;
   delete image;
   GdiplusShutdown(gdiplusToken);
   return 0;
}

程序运行结束,并不能将新添加的属性添加到原有的图片上去。这个问题可以通过image类的另一个获取所有的属性得到验证。

这种方法猜想只是内存中才存在图片的新属性,而不会回写到图片本身。获取需要调用flush之类的操作或者save操作。

下面是之后的思路:

(3)发现确实有Save方法:msdn对Save()方法的调用例子:

VOID Example_SaveFile(HDC hdc)
{
   Graphics graphics(hdc);


   // Create an Image object based on a PNG file.
   Image  image(L"Mosaic.png");


   // Draw the image.
   graphics.DrawImage(&image, 10, 10);


   // Construct a Graphics object based on the image.
   Graphics imageGraphics(&image);


   // Alter the image.
   SolidBrush brush(Color(255, 0, 0, 255));
   imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);


   // Draw the altered image.
   graphics.DrawImage(&image, 200, 10);


   // Save the altered image.
   CLSID pngClsid;
   GetEncoderClsid(L"image/png", &pngClsid);

// Save the image to the same file name. (This operation will fail.)
   image.Save(L"Mosaic2.png", &pngClsid, NULL);
}


对原有的jpg文件处理的时候,所以这里还有有个中间jpg文件。上面有一个很伤的的,GetEncoderClsid(L"image/png", &pngClsid);就是这个函数在库里面并不存在,通过网上查找才知道实际上这个函数是自己实现的,伤啊:

这个函数还包括将加载到内存中的图片以什么格式输出的情况

int   GetEncoderClsid(const   WCHAR*   format,   CLSID*   pClsid) 

UINT     num   =   0;                     //   number   of   image   encoders 
UINT     size   =   0;                   //   size   of   the   image   encoder   array   in   bytes 


ImageCodecInfo*   pImageCodecInfo   =   NULL; 


GetImageEncodersSize(&num,   &size); 
if(size   ==   0) 
return   -1;     //   Failure 


pImageCodecInfo   =   (ImageCodecInfo*)(malloc(size)); 
if(pImageCodecInfo   ==   NULL) 
return   -1;     //   Failure 


GetImageEncoders(num,   size,   pImageCodecInfo); 


for(UINT   j   =   0;   j   <   num;   ++j) 

if(   wcscmp(pImageCodecInfo[j].MimeType,   format)   ==   0   ) 

*pClsid   =   pImageCodecInfo[j].Clsid; 
free(pImageCodecInfo); 
return   j;     //   Success 
}         



free(pImageCodecInfo); 
return   -1;     //   Failure 
}

添加上这个函数就ok了!


至此



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百鸣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值