2016-05-20 4 views
2

Ich benutze WinForms. In meinem Formular habe ich eine Picuturebox und 4 Textboxen margin_top, margin_bottom, margin_left und margin_right. Ich möchte in der Lage sein, Ränder für die Bilder zu erstellen, die in der Druckvorschau-Dialogbox erscheinen, aber ich möchte auch das Bild proportional skalieren, wenn ich die Ränder zur Verfügung stelle. Ich möchte auch, dass die Bilder in der Print-Preview-Seite eingeschlossen sind, was bedeutet, dass die Bilder nicht ausgeschnitten werden. Eine andere Frage ist, warum schneidet mein Bild, wenn die Druckvorschau Seite die gleiche Größe wie mein Bild ist? Ich habe ein Bild mit einer Breite von 850 mal 1100 verwendet, und als ich auf die Druckvorschau geklickt habe, wurde das Bild abgeschnitten, ohne dass ich es neu skalieren musste.Drucken mit Seitenrändern, während das Seitenverhältnis von Bild C beibehalten wird #

Unten ist ein Link für ein Bild, an dem Sie testen können.

http://www.filedropper.com/850x1100

enter image description here

Unten ist ein Bild, das nicht richtig in der Druckvorschau Bildschirm angezeigt wird. Es fehlt seine rechte und untere Grenze.

enter image description here

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Printing_Image_Center 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     pictureBox1.Image = new Bitmap(@"C:\Users\Bob\Pictures\850x1100.png"); 
    } 

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

     var img_width = e.PageBounds.Width - e.MarginBounds.Left - Math.Abs(e.MarginBounds.Right - e.PageBounds.Width); 
     var img_height = e.PageBounds.Height - e.MarginBounds.Top - Math.Abs(e.MarginBounds.Bottom - e.PageBounds.Height); 

     e.Graphics.DrawImage(ResizeAcordingToImage(pictureBox1.Image, img_width, img_height), 
      e.MarginBounds.Left, e.MarginBounds.Top); 
    } 

    private void Btn_Print_Click(object sender, EventArgs e) 
    { 
     printPreviewDialog1.Document = printDocument1; 

     //PrintDocument.OriginAtMargins = true; 
     printDocument1.DefaultPageSettings.Margins.Top = Convert.ToInt32(txt_Top.Text); 
     printDocument1.DefaultPageSettings.Margins.Left = Convert.ToInt32(txt_Left.Text); 
     printDocument1.DefaultPageSettings.Margins.Right = Convert.ToInt32(txt_Right.Text); 
     printDocument1.DefaultPageSettings.Margins.Bottom = Convert.ToInt32(txt_bottom.Text); 

     printPreviewDialog1.ShowDialog(); 
    } 


    private Image ResizeAcordingToImage(Image Source, int boxWidth, int boxHeight) 
    { 
     Image resizedImage; 
     double dbl = (double)Source.Width/(double)Source.Height; 
     //set height of image to boxHeight and check if resulting width is less than boxWidth, 
     //else set width of image to boxWidth and calculate new height 
     if ((int)((double)boxHeight * dbl) <= boxWidth) 
     { 
      resizedImage = new Bitmap(Source, (int)((double)boxHeight * dbl), boxHeight); 
     } 
     else 
     { 
      resizedImage = new Bitmap(Source, boxWidth, (int)((double)boxWidth/dbl)); 
     } 

     return resizedImage; 

    } 
} 
} 

Antwort

1

Bitte geben Sie die Werte von img.width und img.height in Ihrem Code, um das Problem zu lösen. Das ist das Einzige, was du vermisst hast.

var img = ResizeAcordingToImage(pictureBox1.Image, img_width, img_height); 

e.Graphics.DrawImage(img, e.MarginBounds.Left, e.MarginBounds.Top, img.Width, img.Height); 

Hier Ihr Ergebnis nach den Änderungen ist:

enter image description here

+1

Dank Humayun! – taji01

Verwandte Themen