|
|
<火山程序 类型 = "通常" 版本 = 1 />
类 图片字节集缩放工具 <公开 折叠>
{
方法 缩放图片字节集 <公开 静态 类型 = 字节集类>
参数 原图字节集 <类型 = 字节集类>
参数 新宽度 <类型 = 整数>
参数 新高度 <类型 = 整数>
参数 质量 <类型 = 整数>
{
@ Gdiplus::GdiplusStartupInput gdiplusStartupInput;
@ ULONG_PTR gdiplusToken;
@ Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
@
@ if (原图字节集.取字节集长度() == 0)
@ {
@ Gdiplus::GdiplusShutdown(gdiplusToken);
@ return 字节集类();
@ }
@
@ // 创建内存流
@ IStream* pStream = NULL;
@ HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 原图字节集.取字节集长度());
@ if (hGlobal == NULL)
@ {
@ Gdiplus::GdiplusShutdown(gdiplusToken);
@ return 字节集类();
@ }
@
@ void* pData = GlobalLock(hGlobal);
@ if (pData == NULL)
@ {
@ GlobalFree(hGlobal);
@ Gdiplus::GdiplusShutdown(gdiplusToken);
@ return 字节集类();
@ }
@
@ memcpy(pData, 原图字节集.取指针(), 原图字节集.取字节集长度());
@ GlobalUnlock(hGlobal);
@
@ if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK)
@ {
@ GlobalFree(hGlobal);
@ Gdiplus::GdiplusShutdown(gdiplusToken);
@ return 字节集类();
@ }
@
@ // 从流加载图片
@ Gdiplus::Bitmap* srcImage = Gdiplus::Bitmap::FromStream(pStream);
@ if (srcImage == NULL || srcImage->GetLastStatus() != Gdiplus::Ok)
@ {
@ pStream->Release();
@ Gdiplus::GdiplusShutdown(gdiplusToken);
@ return 字节集类();
@ }
@
@ // 创建目标位图
@ Gdiplus::Bitmap* dstBitmap = new Gdiplus::Bitmap(新宽度, 新高度, srcImage->GetPixelFormat());
@ Gdiplus::Graphics graphics(dstBitmap);
@
@ // 设置高质量缩放
@ graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
@ graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
@
@ // 绘制缩放图片
@ graphics.DrawImage(srcImage, 0, 0, 新宽度, 新高度);
@
@ // 保存到内存流
@ IStream* pOutStream = NULL;
@ CreateStreamOnHGlobal(NULL, TRUE, &pOutStream);
@
@ 字节集类 结果字节集;
@
@ if (pOutStream != NULL)
@ {
@ CLSID clsid;
@ if (获取编码器CLSID(L"image/jpeg", &clsid))
@ {
@ Gdiplus::EncoderParameters encoderParams;
@ encoderParams.Count = 1;
@ encoderParams.Parameter[0].Guid = Gdiplus::EncoderQuality;
@ encoderParams.Parameter[0].Type = Gdiplus::EncoderParameterValueTypeLong;
@ encoderParams.Parameter[0].NumberOfValues = 1;
@ encoderParams.Parameter[0].Value = &质量;
@
@ if (dstBitmap->Save(pOutStream, &clsid, &encoderParams) == Gdiplus::Ok)
@ {
@ // 获取流数据
@ STATSTG stat;
@ pOutStream->Stat(&stat, STATFLAG_NONAME);
@
@ HGLOBAL hGlobalOut;
@ GetHGlobalFromStream(pOutStream, &hGlobalOut);
@
@ void* pDataOut = GlobalLock(hGlobalOut);
@ if (pDataOut != NULL)
@ {
@ 结果字节集.分配(stat.cbSize.LowPart);
@ memcpy(结果字节集.取指针(), pDataOut, stat.cbSize.LowPart);
@ GlobalUnlock(hGlobalOut);
@ }
@ }
@ pOutStream->Release();
@ }
@ }
@
@ delete srcImage;
@ delete dstBitmap;
@ pStream->Release();
@ Gdiplus::GdiplusShutdown(gdiplusToken);
@
@ return 结果字节集;
}
方法 获取编码器CLSID <静态 类型 = 逻辑型 折叠>
参数 格式 <类型 = 文本型>
参数 pClsid <类型 = 变整数>
{
@ UINT num = 0;
@ UINT size = 0;
@ Gdiplus::GetImageEncodersSize(&num, &size);
@ if (size == 0)
@ {
@ return false;
@ }
@
@ Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::ImageCodecInfo*)malloc(size);
@ if (pImageCodecInfo == NULL)
@ {
@ return false;
@ }
@
@ Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
@
@ for (UINT j = 0; j < num; ++j)
@ {
@ if (wcscmp(pImageCodecInfo[j].MimeType, 格式.取文本()) == 0)
@ {
@ *((CLSID*)pClsid) = pImageCodecInfo[j].Clsid;
@ free(pImageCodecInfo);
@ return true;
@ }
@ }
@
@ free(pImageCodecInfo);
@ return false;
}
}
|
|