フォームアプリケーションでpictureBoxを印刷しようとしているのですが、
プレビューにて縦に妙に引き延ばされてしまいます。
実際印刷をしていないのでこのままプリントされるかわかりませんが、
フォームで見ているpictureBoxそのままの比率でプレビュー表示、また印刷する方法をご教授お願いします。
C#
1//プレビュー 2private void button1_Click(object sender, EventArgs e) 3{ 4 System.Drawing.Printing.PrintDocument pd =new System.Drawing.Printing.PrintDocument(); 5 pd.PrintPage +=new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage); 6 PrintPreviewDialog ppd = new PrintPreviewDialog(); 7 ppd.Document = pd; 8 ppd.ShowDialog(); 9} 10 11//印刷 12private void button2_Click(object sender, EventArgs e) 13{ 14 PrintDocument pd = new PrintDocument(); 15 pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); 16 pd.Print(); 17} 18 19//GOボタン(pictureboxに表示) 20private void button3_Click(object sender, EventArgs e) 21{ 22 //表示する文字列 23 string print1 = textBox1.Text; 24 string print2 = textBox2.Text; 25 26 Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height); 27 //ImageオブジェクトのGraphicsオブジェクトを作成する 28 Graphics g = Graphics.FromImage(canvas); 29 30 Font fnt = new Font("Arial", 25); 31 StringFormat sf = new StringFormat(); 32 //文字列を描画するときの大きさを計測する 33 SizeF stringSize = g.MeasureString(print1, fnt, 1000, sf); 34 //取得した文字列の大きさを使って四角を描画する 35 g.FillRectangle(Brushes.Black, 0, 0, stringSize.Width, stringSize.Height); 36 37 //文字列を描画する 38 g.DrawString(print1, fnt, Brushes.White, 0, 0, sf); 39 g.DrawString('\n' + print2, fnt, Brushes.Black, 0, 0, sf); 40 41 //リソースを解放する 42 fnt.Dispose(); 43 sf.Dispose(); 44 g.Dispose(); 45 46 //PictureBox1に表示する 47 pictureBox1.Image = canvas; 48} 49 50//printDocument 51private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 52{ 53 var image = pictureBox1.Image; 54 e.Graphics.DrawImage(image,e.MarginBounds); 55}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/25 06:14