locked
GDI+ Image Loader OutOfMemory RRS feed

  • Question

  • I'm using GDI+ to load a small 32x8 png image ind it tells me that its out of memory.  My computer has 6GB of memory and the graphics card has 512MB, so how can it be out of memory?

    Heres the source:

    Image i_shotgun(L"shotgun.png");
    short r=graphics.DrawImage(&i_shotgun,Point(64,0));
    

    i_shotgun.lastResult=OutOfMemory

    r=2

     

    Please help.

    Tuesday, April 5, 2011 1:27 AM

Answers

  • Awful code.

    1) BeginPaint should be called in each paint cycle and only for response to WM_PAINT message. EndPaint should be called at the end of paint cycle.

    2) Do not call anything like Draw in message loop and it should look like this:

    while (GetMessage(&msg, NULL, 0, 0))
    { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg);
    }

    If you want to repaint window or region call InvalidateRect or InvalidateRgn.

    3) Add Graphics graphics(hDC); after BeginPaint call.


    Nikita Leontiev
    • Marked as answer by Rob Pan Tuesday, April 19, 2011 9:18 AM
    Thursday, April 7, 2011 10:01 AM
  • Duplicated Graphics graphics(hdc) in DrawPlayer.

    Your code works for me, except I don't understand invalidating on each mouse move.


    Nikita Leontiev
    • Marked as answer by Rob Pan Tuesday, April 19, 2011 9:18 AM
    Saturday, April 9, 2011 9:11 AM

All replies

  • Status 2 = InvalidParameter, not OutOfMemory.


    Nikita Leontiev
    Tuesday, April 5, 2011 7:42 AM
  • the image loader returns that it is out of memory and doesn't load the picture, DrawImage returns an invalid parameter because the picture wasn't loaded and had a value of 0. 

    i_shotgun = {nativeImage=0x00000000 lastResult=OutOfMemory loadStatus=-858993460 }

    Tuesday, April 5, 2011 10:49 PM
  • Can you post your code?
    Nikita Leontiev
    Wednesday, April 6, 2011 10:20 AM
  • //#include <stdafx.h>
    #include <windows.h>
    #include <windowsx.h>
    #include <objidl.h>
    #include <gdiplus.h>
    #include <math.h>
    #include <vector>
    
    using namespace Gdiplus;
    #pragma comment (lib,"Gdiplus.lib")
    
    #define PI 3.14159265
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    HDC					hdc;
    PAINTSTRUCT			ps;
    
    POINT Mouse;
    
    Graphics* graphics=Graphics::FromHDC(hdc);
    
    bool done=false;
    
    short scale=2;
    
    class PointSet
    {
    public:
    	PointF * points;
    	Color color;
    	int size;
    	PointSet(PointF * pt,Color c,int s)
    	{
    		this->points=pt;
    		this->color=c;
    		this->size=s;
    	}
    };
    
    class Player
    {
    public:
    	REAL x,y;
    	std::vector<PointSet> Model;
    	void AddPointSet(PointF * pt,Color c,int s)
    	{
    		PointSet temp(pt,c,s);
    		Model.push_back(temp);
    	}
    	void DrawPlayer()
    	{
    		SolidBrush Brush(Color(0,0,0,0));
    
    		graphics->TranslateTransform(x,y);
    
    		unsigned int i;
    		for (i=0;i<Model.size();i++)
    		{
    			Brush.SetColor(Model[i].color);
    			graphics->FillPolygon(&Brush,Model[i].points,Model[i].size);
    		}
    	}
    
    };
    
    Player Player1;
    
    PointF test_0[4]={PointF(0.0f,0.0f),PointF(32*scale,0),PointF(32*scale,14*scale),PointF(0,14*scale)};
    PointF test_1[8]={PointF(8*scale,8*scale),PointF(24*scale,8*scale),PointF(24*scale,22*scale),PointF(20*scale,22*scale),PointF(20*scale,26*scale),PointF(12*scale,26*scale),PointF(12*scale,22*scale),PointF(8*scale,22*scale)};
    PointF test_2[12]={PointF(2*scale,26*scale),PointF(30*scale,26*scale),PointF(30*scale,44*scale),PointF(26*scale,44*scale),PointF(26*scale,34*scale),PointF(24*scale,34*scale),PointF(24*scale,44*scale),PointF(8*scale,44*scale),PointF(8*scale,34*scale),PointF(6*scale,34*scale),PointF(6*scale,44*scale),PointF(2*scale,44*scale)};
    Point test_3[8]={Point(8*scale,46*scale),Point(24*scale,46*scale),Point(24*scale,62*scale),Point(18*scale,62*scale),Point(18*scale,50*scale),Point(14*scale,50*scale),Point(14*scale,62*scale),Point(8*scale,62*scale)};
    Point test_4[4]={Point(8*scale,44*scale),Point(24*scale,44*scale),Point(24*scale,46*scale),Point(8*scale,46*scale)};
    Point test_5[4]={Point(6*scale,62*scale),Point(14*scale,62*scale),Point(14*scale,64*scale),Point(6*scale,64*scale)};
    Point test_6[4]={Point(18*scale,62*scale),Point(26*scale,62*scale),Point(26*scale,64*scale),Point(18*scale,64*scale)};
    Point test_7[4]={Point(12*scale,12*scale),Point(14*scale,12*scale),Point(14*scale,14*scale),Point(12*scale,14*scale)};
    Point test_8[4]={Point(18*scale,12*scale),Point(20*scale,12*scale),Point(20*scale,14*scale),Point(18*scale,14*scale)};
    Point test_9[4]={Point(10*scale,12*scale),Point(12*scale,12*scale),Point(12*scale,14*scale),Point(10*scale,14*scale)};
    Point test_10[4]={Point(20*scale,12*scale),Point(22*scale,12*scale),Point(22*scale,14*scale),Point(20*scale,14*scale)};
    
    VOID Init(HWND hWnd)
    {
    	GetCursorPos(&Mouse);
    
    	hdc=BeginPaint(hWnd,&ps);
    
    	graphics=Graphics::FromHDC(hdc);
    
    	Player1.AddPointSet(test_0,Color(255,0,0,0),4);
    	Player1.AddPointSet(test_1,Color(255,239,228,176),8);
    	Player1.AddPointSet(test_2,Color(255,255,0,0),12);
    }
    
    void close(HWND hWnd)
    {
    	EndPaint(hWnd,&ps);
    }
    
    VOID Draw()
    {
    	short x=scale;
    
    	graphics->Clear(Color(255,255,255,255));
    
    	Player1.DrawPlayer();
    	/*
    	double tan_x=((Mouse.y)/(Mouse.x));
    	double angle=atan(tan_x)*180/PI;
    
    	graphics.RotateTransform(angle);
    	*/
    	//SolidBrush Brush(Color(0,0,0,0));
    	/*
    	Brush.SetColor(Color(255,255,0,0));
    	graphics.FillPolygon(&Brush,test_2,12);
    	Brush.SetColor(Color(255,0,0,255));
    	graphics.FillPolygon(&Brush,test_3,8);
    	Brush.SetColor(Color(255,0,0,0));
    	graphics.FillPolygon(&Brush,test_4,4);
    	graphics.FillPolygon(&Brush,test_5,4);
    	graphics.FillPolygon(&Brush,test_6,4);
    	graphics.FillPolygon(&Brush,test_7,4);
    	graphics.FillPolygon(&Brush,test_8,4);
    	Brush.SetColor(Color(255,255,255,255));
    	graphics.FillPolygon(&Brush,test_9,4);
    	graphics.FillPolygon(&Brush,test_10,4);
    
    	Pen pen(Color(255,0,0,0));
    	graphics.DrawRectangle(&pen,0,0,32*x,64*x);
    	*/
    	Image i_shotgun(L"shotgun.png");
    	short r=graphics->DrawImage(&i_shotgun,Point(64,0));
    	/*
    	FontFamily Arialf(L"Arial");
    	Font Arial(&Arialf,64,FontStyleRegular,UnitPixel);
    
    	r=graphics.DrawString(L"Hello World!",-1,&Arial,PointF(64,0),&Brush);
    	*/
    }
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
    {
     HWND    hWnd;
     MSG     msg;
     WNDCLASS   wndClass;
     GdiplusStartupInput gdiplusStartupInput;
     ULONG_PTR   gdiplusToken;
      
     // Initialize GDI+.
     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
     wndClass.style   = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
     wndClass.lpfnWndProc = WndProc;
     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(WHITE_BRUSH);
     wndClass.lpszMenuName = NULL;
     wndClass.lpszClassName = TEXT("Test");
     
     RegisterClass(&wndClass);
     
    	hWnd = CreateWindow(
    		TEXT("Test"), // window class name
    		TEXT("Test"), // window caption
    		WS_OVERLAPPEDWINDOW,  // window style
    		CW_USEDEFAULT,   // initial x position
    		CW_USEDEFAULT,   // initial y position
    		1280,						// initial x size
    		720,						// initial y size
    		NULL,      // parent window handle
    		NULL,      // window menu handle
    		hInstance,    // program instance handle
    		NULL);     // creation parameters
    
    	Init(hWnd);
    	 
    	ShowWindow(hWnd, iCmdShow);
    	UpdateWindow(hWnd);
    	/*
    	while(GetMessage(&msg,NULL,0,0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
     */
    	while (!done)
    	{
    		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    		
    		Draw();
    
    		UpdateWindow(hWnd);
    	}
    
    	close(hWnd);
     GdiplusShutdown(gdiplusToken);
     return msg.wParam;
    } // WinMain
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
     WPARAM wParam, LPARAM lParam)
    {
    	switch(message)
    	{
    	case WM_MOUSEMOVE:
    		Mouse.x=GET_X_LPARAM(lParam);
    		Mouse.y=GET_Y_LPARAM(lParam);
    		return 0;
    	case WM_PAINT:
    		Draw();
    		return 0;
    	case WM_DESTROY:
    		done=true;
    		PostQuitMessage(0);
    		return 0;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    }
    

    Wednesday, April 6, 2011 10:52 PM
  • Awful code.

    1) BeginPaint should be called in each paint cycle and only for response to WM_PAINT message. EndPaint should be called at the end of paint cycle.

    2) Do not call anything like Draw in message loop and it should look like this:

    while (GetMessage(&msg, NULL, 0, 0))
    { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg);
    }

    If you want to repaint window or region call InvalidateRect or InvalidateRgn.

    3) Add Graphics graphics(hDC); after BeginPaint call.


    Nikita Leontiev
    • Marked as answer by Rob Pan Tuesday, April 19, 2011 9:18 AM
    Thursday, April 7, 2011 10:01 AM
  • Sorry, I'm new.  So I did all that, but my original problem is still there.
    Friday, April 8, 2011 1:41 AM
  • Post new version of code and your image (use sendspace.com for example).
    Nikita Leontiev
    Friday, April 8, 2011 8:04 AM
  • Source: http://www.sendspace.com/file/6jggcb

    Image: http://www.sendspace.com/file/f8z9l5

     

    Also, how do I set double buffers?


    Friday, April 8, 2011 10:25 PM
  • Duplicated Graphics graphics(hdc) in DrawPlayer.

    Your code works for me, except I don't understand invalidating on each mouse move.


    Nikita Leontiev
    • Marked as answer by Rob Pan Tuesday, April 19, 2011 9:18 AM
    Saturday, April 9, 2011 9:11 AM