1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
//==============================================================================
// MODULE NAME: SaveBitmapAsPNG.cpp
//
// REVISION HISTORY:
//
// 28 June 2015 Rob Hoech
// Original Release
//
// DESCRIPTION:
//
// This module contains the source code for generic support function
// HRESULT SaveBitmapAsPNG(
// char *FileSpec
// , HBITMAP hBitmap
// );
// Function SaveBitmapAsPNG() saves the bitmap variable specified by operand
// hBitmap to PNG file identified by character array szFileSpecPNG, where
// szFileSpecPNG is the concatenation of the directory path and filename
// components of operand szFileSpec and the extension ".png".
//
// USAGE:
//
// hr = SaveBitmapAsPNG(
// char *FileSpec
// , HBITMAP hBitmap
// );
//
//==============================================================================
#include "stdafx.h"
//--------------------------------------------------------------------------------
// Global Variables:
//--------------------------------------------------------------------------------
extern clasAppInfo *AppInfo;
//--------------------------------------------------------------------------------
// Function Declarations:
//--------------------------------------------------------------------------------
HRESULT SaveBitmapAsPNG(
char *szFileSpec
, HBITMAP hBitmap
)
{
char FileSpecPNG[LenFilespecMAX+1];
char tstr[2000];
//--------------------------------------------------------------------------------
// Support for save to PNG and ICON.
//--------------------------------------------------------------------------------
wchar_t wcstring[LenFilespecMAX+1];
size_t NumCharsConverted;
IWICImagingFactory *piFactory = NULL;
IWICBitmapEncoder *piEncoder = NULL;
IWICBitmapFrameEncode *piBitmapFrame = NULL;
IPropertyBag2 *piPropertybag = NULL;
IWICStream *piStream = NULL;
UINT uiWidth = 640;
UINT uiHeight = 480;
HRESULT hr;
//========================================================================
// Processing.
//========================================================================
//--------------------------------------------------------------------------------
// Create BITMP object.
//--------------------------------------------------------------------------------
BITMAP *pBitmap = new BITMAP;
GetObject(hBitmap,sizeof(BITMAP),pBitmap);
//--------------------------------------------------------------------------------
// Create Output Filename.
//--------------------------------------------------------------------------------
clasFilespecInfo *FileSpecInfo = NULL;
FileSpecInfo = new clasFilespecInfo(szFileSpec);
if ( FileSpecInfo->DirIsValid()
&& FileSpecInfo->FnameIsValid()
&& FileSpecInfo->DirExists()
&& !FileSpecInfo->FnameIsWild()
)
{
strcpy(FileSpecPNG,FileSpecInfo->szDir());
strcat(FileSpecPNG,FileSpecInfo->szNameLessExt());
strcat(FileSpecPNG,".png");
delete FileSpecInfo;
}
else
{
delete FileSpecInfo;
return 0;
}
if (0)
{
//X BITMAP *pBitmap = new BITMAP;
//X GetObject(hBitmap,sizeof(BITMAP),pBitmap);
//X hBitmap->Save(szFspecBitmapSAVE, System::Drawing::Imaging::ImageFormat::Png);
//X delete pBitmap;
}
else
{
//--------------------------------------------------------------------------------
// Processing.
//--------------------------------------------------------------------------------
// Initialize COM
CoInitialize(NULL);
// The factory pointer
piFactory = NULL;
hr = CoCreateInstance(
CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, IID_IWICImagingFactory
, (LPVOID*) &piFactory
);
if (SUCCEEDED(hr))
{
hr = piFactory->CreateStream(&piStream);
}
if (SUCCEEDED(hr))
{
mbstowcs_s(&NumCharsConverted, wcstring, (sizeof FileSpecPNG), FileSpecPNG, strlen(FileSpecPNG)+1);
hr = piStream->InitializeFromFilename(wcstring, GENERIC_WRITE);
}
if (SUCCEEDED(hr))
{
hr = piFactory->CreateEncoder(GUID_ContainerFormatPng, NULL, &piEncoder);
}
if (SUCCEEDED(hr))
{
hr = piEncoder->Initialize(piStream, WICBitmapEncoderNoCache);
}
if (SUCCEEDED(hr))
{
hr = piEncoder->CreateNewFrame(&piBitmapFrame, &piPropertybag);
}
if (SUCCEEDED(hr))
{
//X // This is how you customize the TIFF output.
//X PROPBAG2 option = { 0 };
//X option.pstrName = L"PngCompressionMethod";
//X VARIANT varValue;
//X VariantInit(&varValue);
//X varValue.vt = VT_UI1;
//X varValue.bVal = WICPngCompressionZIP;
//X hr = piPropertybag->Write(1, &option, &varValue);
if (SUCCEEDED(hr))
{
hr = piBitmapFrame->Initialize(piPropertybag);
}
}
if (SUCCEEDED(hr))
{
hr = piBitmapFrame->SetSize(UINT(pBitmap->bmWidth), UINT(pBitmap->bmHeight));
if (!SUCCEEDED(hr))
sprintf(tstr,"SetSize() returned HRESULT = %08x.\n", long(hr));
}
WICPixelFormatGUID formatGUID = GUID_WICPixelFormat24bppBGR;
if (SUCCEEDED(hr))
{
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_WICPixelFormat24bppBGR) ? S_OK : E_FAIL;
}
//X if (SUCCEEDED(hr))
//X {
//X //X UINT cbStride = (pBitmap->bmWidth * pBitmap->bmBitsPixel + 7)/8; /* **WICGetStride** */
//X //X UINT cbBufferSize = pBitmap->bmHeight * cbStride;
//X UINT cbStride = pBitmap->bmWidthBytes;
//X UINT cbBufferSize = pBitmap->bmHeight * cbStride;
//X BYTE *pbBuffer = new BYTE[cbBufferSize];
//X if (pbBuffer != NULL)
//X {
//X //X for (UINT i = 0; i < cbBufferSize; i++)
//X //X pbBuffer[i] = static_cast<BYTE>(rand());
//X //X hr = piBitmapFrame->WritePixels(uiHeight, cbStride, cbBufferSize, pbBuffer);
//X hr = piBitmapFrame->WritePixels(pBitmap->bmHeight, pBitmap->bmWidthBytes, cbBufferSize, pBitmap->bmBits);
//X delete[] pbBuffer;
//X }
//X else
//X {
//X hr = E_OUTOFMEMORY;
//X }
//X }
if (SUCCEEDED(hr))
{
UINT i,j,k;
UINT cbBufferSize = pBitmap->bmHeight*pBitmap->bmWidthBytes;
BYTE *pbBuffer = new BYTE[cbBufferSize];
if (pbBuffer != NULL)
{
BYTE *pByteDST, *pByteSRC, *pB;
pB = (BYTE*)pBitmap->bmBits;
k = 0;
pByteSRC = pB + cbBufferSize - pBitmap->bmWidthBytes;
for (i=0; i<pBitmap->bmHeight; i++)
{
for (j=0; j<pBitmap->bmWidthBytes; j++)
pbBuffer[k++] = *(pByteSRC++);
pByteSRC -= 2*pBitmap->bmWidthBytes;
}
//X for (k=0; k<cbbuffersize; k++)
//x pbBuffer[k] = *(pB++);
hr = piBitmapFrame->WritePixels(pBitmap->bmHeight, pBitmap->bmWidthBytes
, cbBufferSize, pbBuffer);
delete pbBuffer;
}
}
if (SUCCEEDED(hr))
hr = piBitmapFrame->Commit();
if (SUCCEEDED(hr))
hr = piEncoder->Commit();
if (piFactory)
piFactory->Release();
if (piBitmapFrame)
piBitmapFrame->Release();
if (piEncoder)
piEncoder->Release();
if (piStream)
piStream->Release();
delete pBitmap;
return hr;
}
};
|