|
BOOL CPicture::SaveAsBitmap(CString sFilePathName)
) W5 E' ]" @- `& y9 I& K//=============================================================================( T3 t$ |& @1 N3 _. M: n3 t/ q
{% K/ u9 [3 a6 W" G9 \& f
BOOL bResult = FALSE;
- N+ z5 ~( o* P+ @ p* o7 y5 B ILockBytes *Buffer = 0;
6 [5 h5 c; r b" I) x% i1 q IStorage *pStorage = 0;5 q; J0 b, g8 p, H! s) A: S
IStream *FileStream = 0;- M) x6 o, H: @2 y
BYTE *BufferBytes;
5 X+ @, n% p5 g6 \ STATSTG BytesStatistics;
$ H2 h+ p1 `* G+ d" k9 h DWORD OutData;4 e) Z( B3 l+ p9 d; z/ w
long OutStream;; \! j/ c5 I8 M: |& f
CFile BitmapFile; CFileException e;, t2 G9 V0 Q. g" Y
double SkipFloat = 0;
) t) I. b% V! Z/ j DWORD ByteSkip = 0;6 g4 J. i. Q7 z7 H) y
_ULARGE_INTEGER RealData;2 v; c' U6 w7 x% z% `7 X' x
' i5 m9 i' ^4 t/ ^# x3 b" K
( S2 n' z: Q% }: Q2 |
# }% G/ ^$ V$ \ h" [7 D; }6 \* U- t' x
CreateILockBytesOnHGlobal(NULL, TRUE, &Buffer); // Create ILockBytes Buffer+ a) Z8 y" ]2 m$ H6 ^
0 i: j* U( U4 x; Y" ~# S+ g
n& x( z( X" h# h N" n# |. w
0 \4 X1 X1 [ H5 r
HRESULT hr = ::StgCreateDocfileOnILockBytes(Buffer,
d F3 a. t4 ?* Q# ^5 n STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, &pStorage);$ `+ G2 n5 ?/ U! h- P
& s5 n) V4 `9 j' K
9 l9 ~! u: q1 m+ }
- n) m2 I- L8 t- x! y5 l, o
hr = pStorage->CreateStream(LPICTURE,4 }7 \' V" m9 x+ Z2 g3 t
STGM_SHARE_EXCLUSIVE | STGM_CREATE | STGM_READWRITE, 0, 0, &FileStream);/ T# j# q1 r# M
3 u" f5 I: r2 J: d: l8 }
) o& w. r' w% p, L/ V1 E) C6 W. I% h
m_pPict->SaveAsFile(FileStream, TRUE, &OutStream); // Copy Data Stream
# x, x& l; E5 h. ~ z- k FileStream->Release();4 P8 |# j- r8 R; b
pStorage->Release();
6 m8 n/ t. z9 p* ^ k% ] Buffer->Flush();
- a9 J* l8 J, k3 {5 `' t# A* ?" u# q7 \' U$ M
- l& W/ H& f7 ] h
4 O+ w) |. |9 d5 V! c
// Get Statistics For Final Size Of Byte Array
5 g: Z& A( V# G _! D Buffer->Stat(&BytesStatistics, STATFLAG_NONAME);/ \2 Y% s- B6 z! E( e
1 x7 M! _; w. z) w/ \& q) w
' e d8 o5 [$ c$ i" z1 y1 G0 l. U+ `: d1 k, ~
// Cut UnNeeded Data Coming From SaveAsFile() (Leave Only Pure Picture Data)5 k/ X, o5 Y8 A0 Y* |2 G
SkipFloat = (double(OutStream) / 512); // Must Be In a 512 Blocks...
) d: Y+ d$ N* h7 s+ v if(SkipFloat > DWORD(SkipFloat)) ByteSkip = (DWORD)SkipFloat + 1;
! K6 P% h& q o& F* o else ByteSkip = (DWORD)SkipFloat;
& C3 ^: H6 H# z2 o' W ByteSkip = ByteSkip * 512; // Must Be In a 512 Blocks...- c5 t% _( |. ]6 A8 C
3 h+ x* s7 i$ C0 f% s+ |7 e# K
// Find Difference Between The Two Values
9 U3 B0 x {4 F% Q ByteSkip = (DWORD)(BytesStatistics.cbSize.QuadPart - ByteSkip);9 Z' i& f+ T, ~2 Y! W! O
5 @8 a; \. L% |5 I: u) b8 B
" { E" a% y! g& u) x V, ~* b$ s( @+ g, Z
// Allocate Only The Pure Picture Data2 B2 {9 r8 S$ J
RealData.LowPart = 0;
# I" b+ W+ _; i" Q/ h: @, s RealData.HighPart = 0;
9 w# l4 `+ F8 O, A RealData.QuadPart = ByteSkip;1 k' e. l2 U( {) E
BufferBytes = (BYTE*)malloc(OutStream);
' z+ y, {2 G9 t+ M' B if(BufferBytes == NULL)" L' B* V* C, M, f# I
{
4 @) A7 y+ f# k% w1 { Buffer->Release();
9 b5 B" u% W, a7 Q& m8 c, E HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;2 U0 V+ X$ D- M+ y1 d
MessageBoxEx(hWnd, Can not allocate enough memory\\t, ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);; C' f, c% x& s5 `2 z& K" `
}3 @8 p. K) J5 e4 o. M
" q: \" C, I6 c# ~
3 r2 I& z# z' Y8 x9 B- a8 ~5 z
- p E. |: K: ~2 j, x Buffer->ReadAt(RealData, BufferBytes, OutStream, &OutData);% |" @, X+ C$ @
+ o7 S5 b: r) ]7 Q# S. S
" b( w" ?/ q$ D* v) j3 B9 m% L% C1 T1 k
if(BitmapFile.Open(sFilePathName, CFile::typeBinary | CFile::modeCreate | CFile::modeWrite, &e)) Q8 C( I. p# j! k
{
* b7 J8 {5 y3 s* J/ |( ] BitmapFile.Write(BufferBytes, OutData);4 U. H$ k7 a5 h8 b7 d
BitmapFile.Close();
" c) m8 W2 S7 Z4 J- J e bResult = TRUE;5 ^- b: b" N) U8 j
}# ~/ x7 z2 i% s Q7 @/ b6 a R6 E; K
else // Write File Failed...# q/ Q9 I8 h* N
{
, p6 b; t3 ]8 a2 ^4 `7 J TCHAR szCause[255];7 o1 u4 z$ o' t8 A" ? M6 X
e.GetErrorMessage(szCause, 255, NULL);' {0 H# O% E$ ?" m3 n
HWND hWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
. g/ V3 @# u) r6 [& q MessageBoxEx(hWnd, szCause, ERROR_TITLE, MB_OK | MB_ICONSTOP, LANG_ENGLISH);
8 s U; R+ l( U* f8 l7 J/ H7 U bResult = FALSE;
; p( a# M- g: M* u# G# X }
+ j8 i* }+ n- W
/ `$ S6 o+ l$ t$ v Buffer->Release();1 [; W% \, N4 z6 b$ ^
free(BufferBytes);9 L; b& D, Z" T9 X, E5 y
2 P0 ]7 w: j$ c' u: v" x. F
5 v' y: M7 H3 w$ p6 Z/ _% J
' M4 B1 _" S- U/ \. w
return(bResult);, {1 g2 b) c, T; [: s! q$ z' f9 z
} |
|