drew_canitz
August 20th, 2007, 02:33 PM
I'm having a problem with GDI+ displaying some JPEG images that I created using Picasa 3.0. Here an example image.
http://family.canitzdomain.com/images/07_05_06_Alex733.JPG
The image says it's 640x480.
When I do the following...
Bitmap bitmap( L"07_05_06_Alex733.jpg" );
unsigned int x = bitmap.GetWidth();
unsigned int y = bitmap.GetHeight();
Graphics graphics( hWnd );
graphics.DrawImage( &bitmap, 0, 0 );
Even though x and y say 640x480 the image ends up drawing larger than 640x480. It seems like it's drawing 4/3rds its stated size in both width and height.
It works when I force the size like this...
graphics.DrawImage( &bitmap, 0, 0, x, y );
However, when I then try to draw text using the same graphics object, the text is now scaled up too! It's very odd.
Does anyone know what's going on and how I can handle this little bit of excitment?
Thanks
VladimirF
August 20th, 2007, 04:39 PM
...Does anyone know what's going on...Did you set any special mapping mode for your window before creating that Graphics object for it?
drew_canitz
August 21st, 2007, 01:45 PM
no...
#include <Windows.h>
#include <GdiPlus.h>
#include <string>
using namespace Gdiplus;
static ATOM s_wndReg = 0;
LRESULT CALLBACK fnWndProc(
HWND hWnd,
UINT iMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch( iMsg )
{
case WM_CLOSE:
DestroyWindow( hWnd );
return 0; // don't call DefWindowProc
case WM_DESTROY:
PostQuitMessage( 0 );
break;
}
return DefWindowProc( hWnd, iMsg, wParam, lParam );
}
HWND fnCreateWindow( const std::string& caption, int width, int height )
{
HWND hWnd;
WNDCLASSEX wndclass = {0};
RECT wrect = {0}, crect = {0};
DWORD Style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
HINSTANCE hInstance = GetModuleHandle( NULL );
std::string className = "jabba_the_hut";
if( s_wndReg == 0 )
{
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = fnWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
wndclass.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );//GRAY_BRUSH );
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = className.c_str();
wndclass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
s_wndReg = RegisterClassEx( &wndclass );
}
hWnd = CreateWindow( className.c_str(), // window class name
caption.c_str(), // window caption
Style, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
width, // initial x size
height, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL // creation parameters
);
if( !hWnd ) return NULL;
// this part makes the window a little bigger based on the size of the window
// title font etc so that the inner area of the window is window_x by window_y
GetWindowRect( hWnd, &wrect );
GetClientRect( hWnd, &crect );
MoveWindow( hWnd, wrect.left, wrect.top,
width + (wrect.right - wrect.left) - (crect.right - crect.left),
height + (wrect.bottom - wrect.top) - (crect.bottom - crect.top),
TRUE );
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
return hWnd;
}
int fnWaitClose()
{
MSG msg = {0};
while( TRUE )
{
GetMessage( &msg, NULL, 0, 0 );
if( msg.message != WM_QUIT )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else break;
}
return (int)msg.wParam;
}
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL );
{
unsigned int x = 640;
unsigned int y = 480;
Bitmap bitmap( L"07_05_06_Alex733.jpg" );
x = bitmap.GetWidth();
y = bitmap.GetHeight();
unsigned int w = 901;
unsigned int h = 701;
Graphics graphics( fnCreateWindow( "stretch test", w, h ) );
graphics.DrawImage( &bitmap, 0, 0 );
graphics.DrawImage( &bitmap, 0, 0, x, y );
Pen red( Color::Red );
unsigned int count;
for( count = 0; count < w; count += 100 )
graphics.DrawLine( &red, count, 0, count, h );
for( count = 0; count < h; count += 100 )
graphics.DrawLine( &red, 0, count, w, count );
fnWaitClose();
}
GdiplusShutdown( gdiplusToken );
return 0;
}
drew_canitz
August 23rd, 2007, 09:58 AM
I moved the continuation of this thread to "gdi+ drawImage scaling bug 2".