This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Graphics.DrawImage does not resize image to the destination rectangle

Using version 9.3.37.0, if I use DrawImage on AdvancedDrawing.Graphics to drawn an image that is a different DPI than the dpi of the graphics object, DrawImage does not resize the image being drawn like the description says it should. If the image being drawn has the same DPI as the graphics object, then DrawImage DOES resize.
Original Image: attach #1413
When DPIs are different
Graphics.DrawImage output Image when the dpis are different: attach #1411
Bitmap.Draw output when dpis are different: attach #1414

When DPIs are the same:
Graphics.DrawImage output Image when the dpis are the same: attach #1410
Bitmap.Draw output when dpis are the same: attach #1412

Should Graphics.DrawImage be resizing to FIT (as it is) or should it be stretching the image to fit like Bitmap.Draw does? Should Bitmap.Draw be stretching to fit?


Code to reproduce:


//using Aurigma.GraphicsMill
//using Aurigma.GraphicsMill.AdvancedDrawing
//using Aurigma.GraphicsMill.Transforms

void Main()
{
	// Generate Orignal image
	int orgWidth = 800;
	int orgHeight = 400;
	int orgDPI = 150;

	int gutterHeight = 20;
	int outWidth = 400;
	int outHeight = 300;
	int outDPI = 150;

	var orgBitmap = new Bitmap(orgWidth,orgHeight,PixelFormat.Format24bppRgb, RgbColor.LightGreen);
	orgBitmap.DpiX = orgDPI;
	orgBitmap.DpiY = orgDPI;
	using (var g = orgBitmap.GetAdvancedGraphics())
	using (var p = new Pen(RgbColor.Brown, 2f))
	{
		g.DrawRectangle(p, 10, 10, orgWidth - 20, orgHeight - 20);
		g.DrawRectangle(p, 20, 20, 80, 60);
	}
	
	orgBitmap.Save(@"C:\temp\orgImg.png");
	
	// draw the original image onto another bitmap 

	var outputBmp = new  Bitmap(outWidth, outHeight + gutterHeight,PixelFormat.Format24bppRgb, RgbColor.Wheat);
	outputBmp.DpiX = outDPI;
	outputBmp.DpiY = outDPI;
	
	using (var g = outputBmp.GetAdvancedGraphics())
	{
		var drawRect = new System.Drawing.RectangleF(0, 1, outWidth, gutterHeight);
		g.DrawText(
			new BoundedText(
			"Top gutter text",  
			g.CreateFont("Arial", 8), 
			new SolidBrush(RgbColor.Black), 
			drawRect, TextAlignment.Center));
		
		// This will NOT resize the image to fit the destination rectangle 
		// when the orgBitmap's DPI does not match the outputDPI
		g.DrawImage(orgBitmap, new System.Drawing.RectangleF(0, gutterHeight, outWidth, outHeight), 1);
	}
	outputBmp.Save(@"C:\temp\outputBmp.png");
	
	 
	var directOutBmp = new Bitmap(outWidth, outHeight + gutterHeight, PixelFormat.Format24bppRgb, RgbColor.PowderBlue);
	directOutBmp.DpiX = outDPI;
	directOutBmp.DpiY = outDPI;
	// This call WILL resize the orgBitmap to the destination rectangle regardless of if the DPIs match or not
	directOutBmp.Draw(
		orgBitmap, 
		new System.Drawing.Rectangle(0, gutterHeight, outWidth, outHeight), 
		CombineMode.Alpha, 
		1, 
		ResizeInterpolationMode.High);
	directOutBmp.Save(@"C:\temp\directOutBmp.png");
	
}


Hi Todd,

Thanks for the detailed explanation.
Should Graphics.DrawImage be resizing to FIT (as it is) or should it be stretching the image to fit like Bitmap.Draw does? Should Bitmap.Draw be stretching to fit?

Graphics.DrawImage should stretch the image to fit the rectangle passed as an argument. I'll post a ticket to our bug tracker regarding this issue.
Since we have an active maintenance license for 9.x, will this fix be applied to the 9.x version?
Since we have an active maintenance license for 9.x, will this fix be applied to the 9.x version?


We will fix it in the last version 10.x. As your maintenance subscription is active, you are eligible to get a free upgrade. The last version is fully compatible with version 9.