Resizing and Printing an image

so image fits on a page

Page 1 of 1

5 Replies - 10958 Views - Last Post: 01 February 2010 - 01:58 PM Rate Topic: -----

#1 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Resizing and Printing an image

Posted 28 January 2010 - 01:56 PM

Hi,

I would like to print an image from a picturebox control and allow the user to select the printer
and page layout if possible.

But the problem that I am facing is that the image is not fitting to a page and I cant select the appropriate
printer and page layout.

How could i go about resizing/rescaling an image so it fits on the page either landscape or portrait?

Most of my images are 1024 by 768.

i did set the picturebox to the following:
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;



Here is my print code:
private void button1_Click(object sender, EventArgs e)
{	 

   printDocument1.OriginAtMargins = true;
   printPreviewDialog1.ShowDialog();

}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(myPictureBox.Image, 0, 0);
}


I then discovered that i had to to set printPreviewDialog's Document property to printDocument1.

If anyone could assist me with this please, it would be much appreciated.

Thanks in advance.

This post has been edited by MarmiteX1: 28 January 2010 - 02:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Resizing and Printing an image

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Resizing and Printing an image

Posted 28 January 2010 - 05:30 PM

Hope this gives you any idea... I never worked with those printing dialogs before, so I can't really provide you some useful information, but will definitely track this thread to see what some experts might say.

Here's my code:

private void my_print_document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
	Bitmap image = new Bitmap("test1.jpg");
	
	float quotient = 1;
	float margin = 20;

	float page_w = e.PageBounds.Width - (2 * margin);
	float page_h = e.PageBounds.Height - (2 * margin);

	if (image.Width >= image.Height)
	{
		quotient = page_w / image.Width;
	}
	if (image.Width < image.Height)
	{
		quotient =  image.Height / page_h;
	}

	float w = page_w;
	float h = image.Height * quotient;
	
	e.Graphics.DrawImage(image, margin, margin, w, h);
}


Was This Post Helpful? 1
  • +
  • -

#3 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: Resizing and Printing an image

Posted 28 January 2010 - 06:31 PM

So, you could try something like this...

		private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{

			Image i = pictureBox1.Image;

			float newWidth = i.Width * 100 / i.HorizontalResolution;
			float newHeight = i.Height * 100 / i.VerticalResolution;
			
			float widthFactor = newWidth / e.MarginBounds.Width;
			float heightFactor = newHeight / e.MarginBounds.Height;


			if(widthFactor>1 | heightFactor > 1)
			{
				if(widthFactor > heightFactor)
				{
					newWidth = newWidth / widthFactor;
					newHeight = newHeight / widthFactor;
				}
				else
				{
					newWidth = newWidth / heightFactor;
					newHeight = newHeight / heightFactor;
				}
			}
		   
			e.Graphics.DrawImage(i, 0, 0, (int)newWidth, (int)newHeight);

		}
	}



Hope this helps,

Kitt
Was This Post Helpful? 1
  • +
  • -

#4 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Resizing and Printing an image

Posted 30 January 2010 - 01:53 PM

Hi,

Thanks to all for the input. Really appreciate it. I'm just experimenting with the code.

Just a quick question is the widthfactor/height factor used to determine the actual width/height of the image
so it can fit on the page?

Will this work if i crop or rotate an image?

Thanks,
Was This Post Helpful? 0
  • +
  • -

#5 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: Resizing and Printing an image

Posted 01 February 2010 - 09:09 AM

Here are some comments, I should have put them right away...


private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
		{

	Image i = pictureBox1.Image;
	
				// i.HorizontalResolution = pixels per inch of the image
				// i.VerticalResoulutin = ppi of image again

				// e.MarginBounds.Width = printable width in 100 ppi
				// e.MarginBounds.Height = printable height in 100 ppi

				float newWidth = i.Width * 100 / i.HorizontalResolution;  // Convert to same units (100 ppi) as e.MarginBounds.Width
				float newHeight = i.Height * 100 / i.VerticalResolution;   // Convert to same units (100 ppi) as e.MarginBounds.Height
			
	float widthFactor = newWidth / e.MarginBounds.Width; 
	float heightFactor = newHeight / e.MarginBounds.Height;


	if(widthFactor>1 | heightFactor > 1) // if the image is wider or taller than the printable area then adjust...
	{
		if(widthFactor > heightFactor)
		{
			newWidth = newWidth / widthFactor;
			newHeight = newHeight / widthFactor;
		}
		else
		{
			newWidth = newWidth / heightFactor;
			newHeight = newHeight / heightFactor;
		}
	}
		   
	e.Graphics.DrawImage(i, 0, 0, (int)newWidth, (int)newHeight);
	}
}



-Kitt
Was This Post Helpful? 0
  • +
  • -

#6 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Resizing and Printing an image

Posted 01 February 2010 - 01:58 PM

Thanks for the clarification :)

Could you take a look this problem i am having please?
http://www.dreaminco...howtopic=153316

Thanks,

This post has been edited by MarmiteX1: 01 February 2010 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1