GDIplus :: Resizing an Image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    GDIplus :: Resizing an Image

    Hello,

    been a while since I visited bytes.com, but I have an issue with GDIplus...

    I'm currently developing on/for a Windows Server 2003 system and I'm running a Benchmark to find the fastest and most performent way of resizing a jpg file from MegaPixel format to 640 * 480 size.

    Gdiplus currently offers the fastest way for me with a result of 3ms / resize.
    I am however recieving random Access Violations exceptions and the small tool sometimes uses up to 2GB of ram when running.

    I've had serious issues getting Gdiplus to work to the point it is now, but I have no clue whats causing the problem. It usually happens on the SetInterpolatio n function of a Graphic.

    Code:
    	// Prepare GDIplus
    	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    	ULONG_PTR gdiplusToken;
    	Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);
    
    	// Prepare the the variables that we are going to need:
    	time_t startpoint, endpoint, point;
    	Progress->Min = 0;
    	Progress->Max = 999;
    	Progress->Position = 0;
    	double counter = 0;
    
    
    	// Tell the user what this benchmark entails
    	ShowMessage("This benchmark will compress 1 image 1000 times and write it to disk 1000 times using GDI+.");
    
    	// load the image from the disk
    	Gdiplus::Bitmap* original;
    	::LPWSTR name = "C:/test.jpg";
    	original->FromFile(name);
    	
    	// Start the loop and set our start time:
    	startpoint = clock();
    	for(int i = 0; i < 1000; ++i)
    	{
    		// Set our start point
    		point = clock();
    
    		// Create a new bitmap
    		Gdiplus::Bitmap *resized = new Gdiplus::Bitmap(640, 480);
    
    		// Create a new Graphics
    		Gdiplus::Graphics *g;
    		g->FromImage(resized);
    
    		// Set the Interpolationmode
    		g->SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
    
    		// Redraw our image
    		g->DrawImage(original, 0, 0, 0, 0, original->GetWidth(), original->GetHeight(), Gdiplus::UnitPixel);
    		counter += difftime(clock(), point);
    		
    		// Cleanup our mess
    		delete resized;
    
    		// Increment Progress bar and display cpu load
    		Progress->StepBy(1);
    
    		// process messages
    		Application->ProcessMessages();
    	}
    	// fetch our endtime
    	endpoint = clock();
    
    	// Display the result:
    	LOG->Items->Add("GDI+ took about " + FloatToStr(difftime(endpoint, startpoint) / 1000) + " seconds.");
    	LOG->Items->Add("GDI+ :: average copy time = " + FloatToStr(counter / 1000) + " ms.");
    
    	// delete pointer
    	delete original;
    	
    	// Stop Gdiplus
    	Gdiplus::GdiplusShutdown(gdiplusToken);
    Above code is the Benchmark2 function zhich resizes an image 1000 times to calculate the time.
    I'm also developing with Borland in Rad Studio 2007
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    First of all the pointer at line 33 is not initialized. This looks like its pointing to almost anywhere and I'm surprised it works at all.

    The microsoft web site has some examples of resizing images. This might be a faster way to make the conversion. Take a look at this link: http://msdn.microsoft.com/en-us/libr...86(VS.85).aspx

    Comment

    • Airslash
      New Member
      • Nov 2007
      • 221

      #3
      cheers,

      I've got a working version at the moment and using ScaleTransform to resize the images I need.
      Currently needs up to 120ms to resize an image from 3Mpixel size to 640x480 with decent quality.

      I'll take a peek at the link you provided as well.

      Comment

      • RRick
        Recognized Expert Contributor
        • Feb 2007
        • 463

        #4
        Was line 33 part of the problem?

        Comment

        • Airslash
          New Member
          • Nov 2007
          • 221

          #5
          No, strangely enough that line worked perfectly, although I have changed it into something more safe.

          The problem was that GDI+ was unable to find certain identifiers. Solved it by changing the order of #include statements.
          So all works at the moment, now its time for optimalization.

          Comment

          Working...