2012-04-02 12 views
24

फिट करने के लिए छवि को समायोजित करने के लिए कैसे करें, मैं नीचे कोड के साथ PrintDocument क्लास का उपयोग करके एक छवि मुद्रित करने का प्रयास कर रहा हूं। छवि आकार 1200 पीएक्स चौड़ाई और 1800 पीएक्स ऊंचाई है। मैं इस छवि को एक छोटे से ज़ेबरा प्रिंटर का उपयोग करके 4 * 6 पेपर में प्रिंट करने की कोशिश कर रहा हूं। लेकिन कार्यक्रम प्रिंटिंग केवल 4 * 6 बड़ी छवि के हैं। इसका मतलब है कि यह छवि को पेपर आकार में समायोजित नहीं कर रहा है!प्रिंट डॉक्यूमेंट के साथ प्रिंटिंग छवि। कागज आकार

 PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += (sender, args) => 
    { 
      Image i = Image.FromFile("C://tesimage.PNG"); 
      Point p = new Point(100, 100); 
      args.Graphics.DrawImage(i, 10, 10, i.Width, i.Height); 
    }; 
    pd.Print(); 

जब मैं खिड़की मुद्रण का उपयोग करके एक ही छवि प्रिंट (राइट क्लिक करें और प्रिंट का चयन करें, यह कागज का आकार करने के लिए स्वचालित रूप से स्केलिंग और सही ढंग से प्रिंट कर रहा है। इसका मतलब है कि सब कुछ 4 * 6 अखबार में आया था।) मैं करना कैसे करते हैं मेरे सी # कार्यक्रम में वही?

+3

यदि आपको कोई उत्तर पसंद है, तो इसे स्वीकार करें। यह उस व्यक्ति को क्रेडिट देता है जिसने आपको उत्तर दिया और उचित लोगों को खोजने के लिए उत्तर खोजने वाले अन्य लोगों की सहायता करता है –

उत्तर

31

ड्राइमेज विधि में आप जो पैरामीटर पारित कर रहे हैं वह आकार होना चाहिए जो आप छवि के आकार के बजाय पेपर पर छवि चाहते हैं, तो DrawImage कमांड आपके लिए स्केलिंग का ख्याल रखेगा। शायद DrawImage कमांड के निम्न ओवरराइड का उपयोग करने का सबसे आसान तरीका है।

args.Graphics.DrawImage(i, args.MarginBounds); 

नोट: यह करता है, तो छवि का अनुपात आयत के रूप में ही नहीं हैं छवि परिवर्तित कर देगा। छवि और कागज के आकार के आकार पर कुछ सरल गणित आपको एक नया आयताकार बनाने की अनुमति देगा जो छवि को छेड़छाड़ किए बिना कागज की सीमाओं में फिट बैठता है।

+2

यह जवाब होना चाहिए! समाधान के लिए धन्यवाद यह उम्मीद के रूप में काम करता है। – Crushermike

20

बीबीॉय के पहले से ही सभ्य उत्तर पर टंपल नहीं करना है, लेकिन मैंने कोड किया है जो पहलू अनुपात को बनाए रखता है। मैंने उसका सुझाव लिया, इसलिए उसे यहां आंशिक क्रेडिट मिलना चाहिए!

PrintDocument pd = new PrintDocument(); 
pd.DefaultPageSettings.PrinterSettings.PrinterName = "Printer Name"; 
pd.DefaultPageSettings.Landscape = true; //or false! 
pd.PrintPage += (sender, args) => 
{ 
    Image i = Image.FromFile(@"C:\...\...\image.jpg"); 
    Rectangle m = args.MarginBounds; 

    if ((double)i.Width/(double)i.Height > (double)m.Width/(double)m.Height) // image is wider 
    { 
     m.Height = (int)((double)i.Height/(double)i.Width * (double)m.Width); 
    } 
    else 
    { 
     m.Width = (int)((double)i.Width/(double)i.Height * (double)m.Height); 
    } 
    args.Graphics.DrawImage(i, m); 
}; 
pd.Print(); 
+1

अच्छा जवाब, लेकिन मैंने तर्क बदल दिया। Arginbounds args.PageBounds –

2

तुम मेरे कोड का उपयोग कर सकते यहाँ

//Print Button Event Handeler 
private void btnPrint_Click(object sender, EventArgs e) 
{ 
    PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += PrintPage; 
    //here to select the printer attached to user PC 
    PrintDialog printDialog1 = new PrintDialog(); 
    printDialog1.Document = pd; 
    DialogResult result = printDialog1.ShowDialog(); 
    if (result == DialogResult.OK) 
    { 
     pd.Print();//this will trigger the Print Event handeler PrintPage 
    } 
} 

//The Print Event handeler 
private void PrintPage(object o, PrintPageEventArgs e) 
{ 
    try 
    { 
     if (File.Exists(this.ImagePath)) 
     { 
      //Load the image from the file 
      System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\myimage.jpg"); 

      //Adjust the size of the image to the page to print the full image without loosing any part of it 
      Rectangle m = e.MarginBounds; 

      if ((double)img.Width/(double)img.Height > (double)m.Width/(double)m.Height) // image is wider 
      { 
       m.Height = (int)((double)img.Height/(double)img.Width * (double)m.Width); 
      } 
      else 
      { 
       m.Width = (int)((double)img.Width/(double)img.Height * (double)m.Height); 
      } 
      e.Graphics.DrawImage(img, m); 
     } 
    } 
    catch (Exception) 
    { 

    } 
} 
+0

'e.Graphics.DrawImage (img, m); 'मेरी समस्या हल हो गई है .. +1 –

2

उत्तर:

public void Print(string FileName) 
{ 
    StringBuilder logMessage = new StringBuilder(); 
    logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ START - {0} - {1} -------------------]", MethodBase.GetCurrentMethod(), DateTime.Now.ToShortDateString())); 
    logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "Parameter: 1: [Name - {0}, Value - {1}", "None]", Convert.ToString(""))); 

    try 
    { 
     if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected. 

     PrintDocument pd = new PrintDocument(); 

     //Disable the printing document pop-up dialog shown during printing. 
     PrintController printController = new StandardPrintController(); 
     pd.PrintController = printController; 

     //For testing only: Hardcoded set paper size to particular paper. 
     //pd.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478); 
     //pd.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478); 

     pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); 
     pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); 

     pd.PrintPage += (sndr, args) => 
     { 
      System.Drawing.Image i = System.Drawing.Image.FromFile(FileName); 

      //Adjust the size of the image to the page to print the full image without loosing any part of the image. 
      System.Drawing.Rectangle m = args.MarginBounds; 

      //Logic below maintains Aspect Ratio. 
      if ((double)i.Width/(double)i.Height > (double)m.Width/(double)m.Height) // image is wider 
      { 
       m.Height = (int)((double)i.Height/(double)i.Width * (double)m.Width); 
      } 
      else 
      { 
       m.Width = (int)((double)i.Width/(double)i.Height * (double)m.Height); 
      } 
      //Calculating optimal orientation. 
      pd.DefaultPageSettings.Landscape = m.Width > m.Height; 
      //Putting image in center of page. 
      m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height)/2); 
      m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width)/2); 
      args.Graphics.DrawImage(i, m); 
     }; 
     pd.Print(); 
    } 
    catch (Exception ex) 
    { 
     log.ErrorFormat("Error : {0}\n By : {1}-{2}", ex.ToString(), this.GetType(), MethodBase.GetCurrentMethod().Name); 
    } 
    finally 
    { 
     logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ END - {0} - {1} -------------------]", MethodBase.GetCurrentMethod().Name, DateTime.Now.ToShortDateString())); 
     log.Info(logMessage.ToString()); 
    } 
} 
+0

मेरे ऊपर कोड परीक्षण और मेरे wpf kisok एप्लिकेशन में ठीक काम कर रहा है। – KhanSahib

3

समाधान Bboy द्वारा प्रदान की ठीक काम करता है। लेकिन मेरे मामले में मुझे

e.Graphics.DrawImage(memoryImage, e.PageBounds); 

यह केवल फ़ॉर्म को प्रिंट करेगा। जब मैं मार्जिनबाउंड का उपयोग करता हूं तो यह पूरी स्क्रीन प्रिंट करता है भले ही फॉर्म मॉनीटर स्क्रीन से छोटा हो। पेजबाउंड ने उस मुद्दे को हल किया। बीबीॉय के लिए धन्यवाद!

+0

सही, मेरे लिए काम किया! (@ बी बॉय उत्तर भी अच्छा है) – iedmrc99

4

टोनीएम और बीबीॉय के साथ सहमत हैं - यह मूल 4 * 6 लेबल के मुद्रण के लिए सही जवाब है। (Args.PageBounds)। यह एंडिसिया एपीआई सेवा शिपिंग लेबल मुद्रण के लिए मेरे लिए काम किया।

private void SubmitResponseToPrinter(ILabelRequestResponse response) 
    { 
     PrintDocument pd = new PrintDocument(); 
     pd.PrintPage += (sender, args) => 
     { 
      Image i = Image.FromFile(response.Labels[0].FullPathFileName.Trim()); 
      args.Graphics.DrawImage(i, args.PageBounds); 
     }; 
     pd.Print(); 
    } 
+0

'पेजबाउंड्स' ने मेरे लिए काम किया जहां 'मार्जिनबाउंड' बहुत छोटा प्रिंटिंग कर रहा था। धन्यवाद! – Goose

संबंधित मुद्दे