changing the image DPI keeping the same size
-
I have a picture taken from printscreen which has the dpi=96 and size is 5 by 5 inch. Now I want to make the image dpi=1200 keeping the same size. How can I do this in C#? Please help me out with some code sample. Thanks in advance :) Faysal
Hi, First you have to know that, as much as I know, in GDI+, the exact size of the image is in pixels and it is being calculated by (DPI * demention(in pixel unit)) so you have to calculate the current size in pixel, then calculate the new size and then use this code to create the new image by new size and then set the new DPI:
Bitmap screenshotBitmap = new Bitmap("c:\\image.jpg"); Bitmap newBitmap = new Bitmap(screenshotBitmap, newWidth, newheight); newBitmap.SetResolution(1200, 1200);
I remember that in some GDI+ coding I had the same problem and I used somthing like this and it worked for me. Good luck and have fun. ;)Sojaner!
-
Hi, First you have to know that, as much as I know, in GDI+, the exact size of the image is in pixels and it is being calculated by (DPI * demention(in pixel unit)) so you have to calculate the current size in pixel, then calculate the new size and then use this code to create the new image by new size and then set the new DPI:
Bitmap screenshotBitmap = new Bitmap("c:\\image.jpg"); Bitmap newBitmap = new Bitmap(screenshotBitmap, newWidth, newheight); newBitmap.SetResolution(1200, 1200);
I remember that in some GDI+ coding I had the same problem and I used somthing like this and it worked for me. Good luck and have fun. ;)Sojaner!
Thanks for the reply. I did it. the original size of my image is 695x700. So I made a new bitmap with the dimension: width= (1200/96)*695 and height= (1200/96)*700. Here my printer's device dpi is 1200 and the screen resolution is 96. Actually I wanted to do this thing for printing the form with high quality. Cause with the screen resolution ( 96dpi ) the printer output is very poor. But at that time while printing the new bitmap my printer stops responding and if I want to see the print preview window it shows an exception. I think it's totally uneffieicnt. Please tell me a way to print my form screenshot with a high quality in C#. Code Snippet Bitmap memoryImage; Bitmap testImage; Graphics gfxScreenshot; private void CaptureScreen() { memoryImage = new Bitmap(this.Width-5,this.Height-100); gfxScreenshot = Graphics.FromImage(memoryImage); gfxScreenshot.CopyFromScreen(this.Location.X+5, this.Location.Y+100, 0, 0, this.Size, CopyPixelOperation.SourceCopy); testImage = new Bitmap(memoryImage, 6950,7000); testImage.SetResolution(1200,1200); } private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImageUnscaled(testImage, new Point(100, 100)); } Thanks for the help... :)
-
Thanks for the reply. I did it. the original size of my image is 695x700. So I made a new bitmap with the dimension: width= (1200/96)*695 and height= (1200/96)*700. Here my printer's device dpi is 1200 and the screen resolution is 96. Actually I wanted to do this thing for printing the form with high quality. Cause with the screen resolution ( 96dpi ) the printer output is very poor. But at that time while printing the new bitmap my printer stops responding and if I want to see the print preview window it shows an exception. I think it's totally uneffieicnt. Please tell me a way to print my form screenshot with a high quality in C#. Code Snippet Bitmap memoryImage; Bitmap testImage; Graphics gfxScreenshot; private void CaptureScreen() { memoryImage = new Bitmap(this.Width-5,this.Height-100); gfxScreenshot = Graphics.FromImage(memoryImage); gfxScreenshot.CopyFromScreen(this.Location.X+5, this.Location.Y+100, 0, 0, this.Size, CopyPixelOperation.SourceCopy); testImage = new Bitmap(memoryImage, 6950,7000); testImage.SetResolution(1200,1200); } private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.Graphics.DrawImageUnscaled(testImage, new Point(100, 100)); } Thanks for the help... :)
I have no experience in printing using C# and/or any other programing language but this is the result of the search I had for you about the DocumentPrint the main class that makes you able to print using the .Net framework:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;public class PrintingExample : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
private Font printFont;
private StreamReader streamToPrint;public PrintingExample() : base() { // The Windows Forms Designer requires the following call. InitializeComponent(); } // The Click event is raised when the user clicks the Print button. private void printButton\_Click(object sender, EventArgs e) { try { streamToPrint = new StreamReader ("C:\\\\My Documents\\\\MyFile.txt"); try { printFont = new Font("Arial", 10); PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler (this.pd\_PrintPage); pd.Print(); } finally { streamToPrint.Close(); } } catch(Exception ex) { MessageBox.Show(ex.Message); } } // The PrintPage event is raised for each page to be printed. private void pd\_PrintPage(object sender, PrintPageEventArgs ev) { float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; string line = null; // Calculate the number of lines per page. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Print each line of the file. while(count < linesPerPage && ((line=streamToPrint.ReadLine()) != null)) { yPos = topMargin + (count \* printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } // If more lines exist, print another page. if(line != null) ev.HasMorePages = true; else ev.HasMorePages = false; } //