使用WIC组件转换图片格式
#include <windows.h>
#include <Wincodec.h>
#pragma comment(lib, "Windowscodecs.lib" )
HRESULT PNG2WDP(WCHAR* szPngFileName, WCHAR* szWdpFileName)
{
IWICImagingFactory *piFactory = NULL;
IWICBitmapEncoder *piEncoder = NULL;
IWICBitmapDecoder *piDecoder = NULL;
IWICBitmapFrameEncode *piBitmapFrame = NULL;
IWICBitmapFrameDecode *piBitmapFrameIn = NULL;
IPropertyBag2 *pPropertybag = NULL;
IWICStream *piStream = NULL;
IWICStream *piStreamIn = NULL;
UINT uiWidth = 0;
UINT uiHeight = 0;
ULONG counter = 0;
CoInitialize(NULL);
HRESULT hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*) &piFactory);
if (SUCCEEDED(hr))
{
hr = piFactory->CreateStream(&piStream);
}
if (SUCCEEDED(hr))
{
hr = piStream->InitializeFromFilename(szWdpFileName, GENERIC_WRITE);
}
if (SUCCEEDED(hr))
{
hr = piFactory->CreateStream(&piStreamIn);
}
if (SUCCEEDED(hr))
{
hr = piStreamIn->InitializeFromFilename(szPngFileName, GENERIC_READ);
}
if (SUCCEEDED(hr))
{
hr = piFactory->CreateEncoder(GUID_ContainerFormatWmp, NULL, &piEncoder);
}
if (SUCCEEDED(hr))
{
hr = piEncoder->Initialize(piStream, WICBitmapEncoderNoCache);
}
if (SUCCEEDED(hr))
{
hr = piEncoder->CreateNewFrame(&piBitmapFrame, &pPropertybag);
}
if(SUCCEEDED(hr))
{
hr = piFactory->CreateDecoder(GUID_ContainerFormatPng, NULL, &piDecoder);
}
if (SUCCEEDED(hr))
{
hr = piDecoder->Initialize(piStreamIn, WICDecodeMetadataCacheOnDemand);
}
if(SUCCEEDED(hr))
{
hr = piDecoder->GetFrame(0, &piBitmapFrameIn);
}
if (SUCCEEDED(hr))
{
// This is how you customize the TIFF output.
PROPBAG2 option = { 0 };
//option.pstrName = L"TiffCompressionMethod";
//VARIANT varValue;
//VariantInit(&varValue);
//varValue.vt = VT_UI1;
//varValue.bVal = WICTiffCompressionZIP;
//hr = pPropertybag->Write(1, &option, &varValue);
if (SUCCEEDED(hr))
{
hr = piBitmapFrame->Initialize(pPropertybag);
}
}
if (SUCCEEDED(hr))
{
hr = piBitmapFrameIn->GetSize(&uiWidth, &uiHeight);
hr = piBitmapFrame->SetSize(uiWidth, uiHeight);
}
WICPixelFormatGUID formatGUID = GUID_WICPixelFormat32bppBGRA;
if (SUCCEEDED(hr))
{
hr = piBitmapFrameIn->GetPixelFormat(&formatGUID);
hr = piBitmapFrame->SetPixelFormat(&formatGUID);
}
if (SUCCEEDED(hr))
{
// We're expecting to write out 24bppRGB. Fail if the encoder cannot do it.
hr = IsEqualGUID(formatGUID, GUID_WICPixelFormat32bppBGRA) ? S_OK : E_FAIL;
if( FAILED(hr) )
{
hr = IsEqualGUID(formatGUID, GUID_WICPixelFormat24bppBGR) ? S_OK : E_FAIL;
}
}
if (SUCCEEDED(hr))
{
{
IWICBitmap *pIBitmap = NULL;
IWICBitmapLock *pILock = NULL;
WICRect rcLock = { 0, 0, uiWidth, uiHeight };
// Create the bitmap from the image frame.
if (SUCCEEDED(hr))
{
hr = piFactory->CreateBitmapFromSource(
piBitmapFrameIn, // Create a bitmap from the image frame
WICBitmapCacheOnDemand, // Cache metadata when needed
&pIBitmap); // Pointer to the bitmap
hr = pIBitmap->Lock(&rcLock, WICBitmapLockWrite, &pILock);
BYTE *pv = NULL;
UINT cbStride = 0;
UINT cbBufferSize = 0;
// Retrieve a pointer to the pixel data.
if (SUCCEEDED(hr))
{
hr = pILock->GetDataPointer(&cbBufferSize, &pv);
}
cbStride = cbBufferSize / uiHeight;
hr = piBitmapFrame->WritePixels(uiHeight, cbStride, cbBufferSize, pv);
counter = pILock->Release();
counter = pIBitmap->Release();
pv = NULL;
}
}
// else
{
// hr = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hr))
{
hr = piBitmapFrame->Commit();
}
if (SUCCEEDED(hr))
{
hr = piEncoder->Commit();
}
if (piFactory)
counter = piFactory->Release();
if (piBitmapFrame)
counter = piBitmapFrame->Release();
if(piBitmapFrameIn)
counter = piBitmapFrameIn->Release();
if(piDecoder)
counter = piDecoder->Release();
if (piEncoder)
counter = piEncoder->Release();
if (piStream)
counter = piStream->Release();
if(piStreamIn)
counter = piStreamIn->Release();
if( pPropertybag )
counter = pPropertybag->Release();
CoUninitialize();
return hr;
}
bool IsRGBA(const std::wstring& strFileName)
{
unsigned char buf[4] = {0};
std::fstream fs(strFileName, std::ios::in | std::ios::binary);
fs.seekg(4, SEEK_SET);
fs.read((char*)&buf, 4);
int pos = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + buf[0];
while( pos > 0 )
{
fs.seekg(pos, SEEK_SET);
fs.read((char*)&buf, 2);
int numEntry = (buf[1] << 8) + buf[0];
fs.seekg(12 * numEntry, SEEK_CUR);
fs.read((char*)&buf, 4);
pos = (buf[3] << 24) + (buf[2] << 16) + (buf[1] << 8) + buf[0];
}
fs.seekg(15, SEEK_CUR);
fs.read((char*)&buf, 1);
if( buf[0] == 0x0F )
return true;
else
return false;
}
使用WIC组件转换图片格式的更多相关文章
- 使用IMAGEMAGICK的CONVERT工具批量转换图片格式
使用IMAGEMAGICK的CONVERT工具批量转换图片格式 http://www.qiansw.com/linux-imagemagick-convert-img.html Home > 文 ...
- java批量转换图片格式
废话不多直接上代码,代码其实也不多.... package com.qiao.testImage; import java.awt.image.BufferedImage; import java.i ...
- Mac电脑如何转换图片格式?ImageWell for Mac转换图片格式教程
想用Mac电脑转换图片格式?我想你可以借助ImageWell for Mac软件!ImageWell是一款简单好用的的图像处理工具,具有显示,编辑,处理,保存等功能.下面小编来为大家演示在Mac电脑上 ...
- 【最简单】不用ps也可以批量转换图片格式
不废话直接开始~ 1.新建文件夹,把需要转换的图片放进去,如图: 2.文件夹里建一txt文本,重点来了!txt文本的内容,如果是jpg转为png,则输入“ren *.jpg *.png”,同理png转 ...
- ubuntu 转换图片格式的方法(sam2p, imagemagick)
(1) 终端:sudo apt-get install sam2p sam2p [原图片名.格式] [目标图片名.格式] 即可在同一目录下生成目标图片格式 (2) 终端: sudo apt-get i ...
- FreeImage库如何转换图片格式?
FreeImage下载地址:http://freeimage.sourceforge.net/ //freeimagemain.h #ifndef FREEIMAGEMAIN_H #define FR ...
- python转换图片格式
在图片所在的路径下,打开命令窗口 bmeps -c picturename.png picturename.eps
- 利用C#转换图片格式及转换为ico
注意:转换为ICO后效果不好. 源代码: using System;using System.Collections.Generic;using System.Text;using System.Dr ...
- ImageMagick 转换图片格式
[root@ drawable-hdpi-v4]# convert ic_launcher.jpeg ic_launcher.png [root@ drawable-hdpi-v4]# file ic ...
随机推荐
- docker images之间相互通信 link
同一个host上的两个container 首先启动一个nginx. container起名叫netease_nginx docker run --detach --name netease_nginx ...
- 【转】PHP实现连贯操作
[第一种方案 __call] 我们在使用一些框架(如ThinkPHP)编码的时候,常用到这样的代码. M('User')->where(array('id'=>1))->field( ...
- windows系统调用 获取当前内存信息
#include "iostream" #include "windows.h" #include "shlwapi.h" #include ...
- Extjs 3.4 复选框的,默认选中 ,禁用,(纯属于自己代码中需要,总结!)
var sm = new Ext.grid.CheckboxSelectionModel( { //一个特定的选择模型,它将渲染一列复选框,可以用来选择或反选多行数据. ...
- 【转】PowerShell入门(五):Cmd命令与PowerShell命令的交互
转至:http://www.cnblogs.com/ceachy/archive/2013/02/18/Call_Between_Cmd_And_PowerShell.html 单独使用一种脚本来完成 ...
- centos 更新python
1.CentOS安装Python的依赖包 yum groupinstall "Development tools"yum install zlib-devel bzip2-deve ...
- asp.net 验证正则表达式
基本元字符: . 任意的一个非换行字符 [] 集合匹配,匹配一个[]中出现的字符. 是在多个字符中取一个. () 调整优先级的作用. 还有一个分组的作用 | 或的意思,测试|一下. 注意,或的优先级最 ...
- [JavaScript]JavaScript处理iframe的动作
随着W3C一声令下,几年前使用非常频繁的frameset + frame已完成使命,光荣退伍.作为frameset的替代方案(姑且这么称吧),iframe的使用也多了起来.较frameset方案,if ...
- MyEclipse 10.7 添加JBOSS 6应用服务器
首先,确保你的JAVA和JBOSS的环境变量配置正确然后,在myeclipse中,window->preferences->myeclipse->servers->JBOSS- ...
- ArcGIS Javascript查询数据库并添加到地图上
将数据存放到数据库中,动态的调取比较灵活,数据变动后不需要改变图层的属性表. 此处采用的方法是通过jquery查询数据库,并将数据库的结果生产json串返回给js,在js中动态解析json串增加点至地 ...