📄 win32bitmap.cpp
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006 Daniel Lindstr鰉 and Daniel Nilsson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "StdAfx.h"
#include "Win32Bitmap.h"
#include "win32_drawing.h" // needed for g_hWndDrawingWindow
//#include <imaging.h>
#include <initguid.h>
#include <olectl.h>
#include <ole2.h>
//#include <imgguids.h>
HBITMAP LoadAnImage(WCHAR* FileName);
Win32Bitmap::Win32Bitmap(WCHAR *filename, bool rotate) {
HBITMAP temp = LoadAnImage(filename);
if(temp == NULL)
throw "Could not load image.";
BITMAP bm;
GetObject(temp, sizeof(bm), (void *)&bm);
this->height = rotate ? bm.bmWidth : bm.bmHeight;
this->width = rotate ? bm.bmHeight : bm.bmWidth;
HDC winhdc = GetDC(g_hWndDrawingWindow);
hdc = CreateCompatibleDC(winhdc);
HDC hdc2 = CreateCompatibleDC(winhdc);
this->bitmap = CreateCompatibleBitmap(winhdc, this->width, this->height);
SelectObject(hdc2, temp);
SelectObject(hdc, bitmap);
POINT p[3];
p[0].x = this->width;
p[0].y = 0;
p[1].x = this->width;
p[1].y = this->height;
p[2].x = 0;
p[2].y = 0;
if(rotate)
PlgBlt(hdc, p, hdc2, 0, 0, this->height, this->width, 0, 0, 0);
else
BitBlt(hdc, 0, 0, this->width, this->height, hdc2, 0, 0, SRCCOPY);
}
Win32Bitmap::~Win32Bitmap(void) {
DeleteDC(hdc);
DeleteObject(bitmap);
}
HDC Win32Bitmap::getHDC() {
return hdc;
}
int Win32Bitmap::getWidth() {
return width;
}
int Win32Bitmap::getHeight() {
return height;
}
// Function LoadAnImage: accepts a file name and returns a HBITMAP.
// On error, it returns 0.
HBITMAP LoadAnImage(WCHAR* FileName)
{
// Use IPicture stuff to use JPG / GIF files
IPicture* p;
IStream* s;
// IPersistStream* ps;
HGLOBAL hG;
void* pp;
FILE* fp;
// Read file in memory
fp = _wfopen(FileName,L"rb");
if (!fp)
return NULL;
fseek(fp,0,SEEK_END);
int fs = ftell(fp);
fseek(fp,0,SEEK_SET);
hG = GlobalAlloc(GPTR,fs);
if (!hG)
{
fclose(fp);
return NULL;
}
pp = (void*)hG;
fread(pp,1,fs,fp);
fclose(fp);
// Create an IStream so IPicture can
CreateStreamOnHGlobal(hG,false,&s);
if (!s)
{
GlobalFree(hG);
return NULL;
}
OleLoadPicture(s,0,false,IID_IPicture,(void**)&p);
if (!p)
{
s->Release();
GlobalFree(hG);
return NULL;
}
s->Release();
GlobalFree(hG);
HBITMAP hB = 0;
p->get_Handle((unsigned int*)&hB);
// Copy the image. Necessary, because upon p's release,
// the handle is destroyed.
HBITMAP hBB = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,
LR_COPYRETURNORG);
p->Release();
return hBB;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -