gdiplus图片格式转换

还没来得及kan ,先转了再说


vc6.0必须装gdiplus库文件和头文件

Given an input stream of some type of image, the followingfunction converts that image into another type of image given thedestination format. The destination format should be a propermime-type such as "image/jpeg", "image/png".

Gdiplus::Status imageToImage(
  IStream *pStreamIn, IStream *pStreamOut, BSTRwszOutputMimeType)
{
    namespace G= Gdiplus;
    G::Statusstatus = G::Ok;
    G::ImageimageSrc(pStreamIn);
    status =imageSrc.GetLastStatus();
    if (G::Ok !=status) {
     return status;
    }
    UINTnumEncoders = 0;
    UINTsizeEncodersInBytes = 0;
    status =G::GetImageEncodersSize(&numEncoders,&sizeEncodersInBytes);
    if (status!= G::Ok) {
     return status;
    }
   G::ImageCodecInfo *pImageCodecInfo =
        (G::ImageCodecInfo *) malloc(sizeEncodersInBytes);
    status =G::GetImageEncoders(
         numEncoders, sizeEncodersInBytes, pImageCodecInfo);
    if (status!= G::Ok) {
     return status;
    }
    CLSIDclsidOut;
    status =G::UnknownImageFormat;
    for (UINT j= 0; j < numEncoders; j ++) {
     if (0 == wcscmp(pImageCodecInfo[j].MimeType, wszOutputMimeType)){
       clsidOut = pImageCodecInfo[j].Clsid;
       status = G::Ok;
       break;
       
    }
   free(pImageCodecInfo);
    if (status!= G::Ok) {
     return status;
    }
    returnimageSrc.Save(pStreamOut, &clsidOut);
  }

It can be used like so:

extern IStream *pSomeImg; // source image format is notimportant
extern IStream *pMyJpeg;
if (Gdiplus::Ok == imageToImage(pSomeImg, pMyJpeg, L"image/jpeg")){
// pMyJpeg holds the converted jpeg.
}

If there is a need to put/retrieve data into/from IStream inbyte-array format (such as char*), it can by done by usingCreateStreamOnHGlobal, GlobalAlloc, GlobalLock Win32 API functions.See this tip for more details
Note: List of supported formats: BMP, ICON, GIF, JPEG, Exif, PNG,TIFF, WMF, and EMF.



摘自:http://blog.sina.com.cn/s/blog_ad0672d601017qpn.html

将字节流转换为YUY2格式的图片需要以下步骤: 1. 读取字节流中的像素数据,根据像素格式将其转换为Y、U、V三个分量的数据。 2. 将Y、U、V三个分量的数据按照YUY2格式交错存储,即先存储一个Y值,接着存储一个U或V值,再存储一个Y值,接着存储一个U或V值,以此类推。 3. 创建一个Gdiplus::Bitmap对象,并将交错存储的YUY2数据传递给它。 4. 将Bitmap对象保存为YUY2格式的图像文件。 下面是一个示例代码,可以将字节流转换为YUY2格式的图像文件: ```c++ #include <windows.h> #include <gdiplus.h> #include <fstream> using namespace Gdiplus; void ByteStreamToYuy2Image(BYTE* byteStream, int width, int height, const char* fileName) { // 计算图像数据的大小 int dataSize = width * height * 2; // 将像素数据转换为Y、U、V三个分量的数据 BYTE* yData = byteStream; BYTE* uData = byteStream + width * height; BYTE* vData = byteStream + width * height * 3 / 2; // 创建一个YUY2格式的图像 Bitmap bitmap(width, height, PixelFormat16bppYUV); BitmapData bitmapData; Rect rect(0, 0, width, height); bitmap.LockBits(&rect, ImageLockModeWrite, PixelFormat16bppYUV, &bitmapData); // 将Y、U、V三个分量的数据按照YUY2格式交错存储 BYTE* dst = (BYTE*)bitmapData.Scan0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j += 2) { *dst++ = yData[i * width + j]; *dst++ = uData[i * width / 2 + j / 2]; *dst++ = yData[i * width + j + 1]; *dst++ = vData[i * width / 2 + j / 2]; } } // 保存图像文件 CLSID clsid; GetEncoderClsid(L"image/yuy2", &clsid); bitmap.Save(CA2W(fileName), &clsid, NULL); bitmap.UnlockBits(&bitmapData); } int main() { // 读取字节流数据 std::ifstream file("input.bin", std::ios::binary); std::vector<BYTE> byteStream((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); // 将字节流转换为YUY2格式的图像 ByteStreamToYuy2Image(&byteStream[0], 640, 480, "output.yuy2"); return 0; } ``` 其中,GetEncoderClsid是一个辅助函数,用于获取指定格式的图像编码器的CLSID,可以通过以下代码实现: ```c++ 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; pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); if (pImageCodecInfo == NULL) return -1; GetImageEncoders(num, size, pImageCodecInfo); for (UINT i = 0; i < num; i++) { if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0) { *pClsid = pImageCodecInfo[i].Clsid; free(pImageCodecInfo); return i; } } free(pImageCodecInfo); return -1; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值