私はWinFormsを使用しています。私のフォームでは、イメージ・ドキュメントを表示するために使用するピクチャ・ボックスがあります。問題は、画像をトリミングしてから文書を印刷して画像が少し歪んだ場合です。私が画像文書をトリミングせずに定期的に印刷すると、画像文書が歪んでしまうことはありません。歪みのない画像文書の切り抜きと印刷C#
イメージドキュメントを歪ませずにトリミングして印刷するにはどうすればよいですか?
これをコードするより良い方法がありますので、イメージドキュメントが歪まずにトリミングして印刷することができますか?もしそうなら、どうすればいいの?画像文書寸法の
例:X 3100 2500
マイ
注:私はで動作する画像が大きいため
は私のPictureBoxをズームに設定されていますpictureboxには枠線がありません
int _cropX, _cropY, _cropWidth, _cropHeight; public Pen _cropPen; private State _currentState; private enum State { Crop } private void Open_btn_Click(object sender, EventArgs e) { // open file dialog OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogResult.OK) { // display image in picture box pictureBox1.Image = new Bitmap(open.FileName); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { try { if (Crop_Checkbox.Checked == true) { Cursor = Cursors.Default; if (_currentState == State.Crop) { if (_cropWidth < 1) { return; } Rectangle rect = new Rectangle(_cropX, _cropY, _cropWidth, _cropHeight); //First we define a rectangle with the help of already calculated points Bitmap originalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height); //Original image Bitmap img = new Bitmap(_cropWidth, _cropHeight); // for cropinf image Graphics g = Graphics.FromImage(img); // create graphics g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; //set image attributes g.DrawImage(originalImage, 0, 0, rect, GraphicsUnit.Pixel); pictureBox1.Image = img; pictureBox1.Width = img.Width; pictureBox1.Height = img.Height; } } else { Cursor = Cursors.Default; } } catch (Exception) { } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (Crop_Checkbox.Checked == true) { if (_currentState == State.Crop) { Cursor = Cursors.Cross; if (e.Button == System.Windows.Forms.MouseButtons.Left) { //X and Y are the coordinates of Crop pictureBox1.Refresh(); _cropWidth = e.X - _cropX; _cropHeight = e.Y - _cropY; pictureBox1.CreateGraphics().DrawRectangle(_cropPen, _cropX, _cropY, _cropWidth, _cropHeight); } } } else { Cursor = Cursors.Default; } } private void Crop_Checkbox_CheckedChanged(object sender, EventArgs e) { if (Crop_Checkbox.Checked == true) { this.Cursor = Cursors.Cross; } } private void Print_btn_Click(object sender, EventArgs e) { System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument(); PrintDialog myPrinDialog1 = new PrintDialog(); myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage); myPrinDialog1.Document = myPrintDocument1; if (myPrinDialog1.ShowDialog() == DialogResult.OK) { myPrintDocument1.Print(); } } private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawImage(pictureBox1.Image, 10, 10); //(Standard paper size is 850 x 1100 or 2550 x 3300 pixels) } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (Crop_Checkbox.Checked == true) { if (_currentState == State.Crop) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { Cursor = Cursors.Cross; _cropX = e.X; _cropY = e.Y; _cropPen = new Pen(Color.FromArgb(153, 180, 209), 3); //2 is Thickness of line _cropPen.DashStyle = DashStyle.DashDotDot; pictureBox1.Refresh(); } } } else { Cursor = Cursors.Default; } }
テスト:やや歪んだ:
テスト:歪まない:
試験: 上の写真は、試験です。
Bitmap originalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
および/置き換え(pictureBox1.ImageにoriginalImage)、編集:
g.DrawImage(pictureBox1.Image, 0, 0, rect, GraphicsUnit.Pixel);
最も可能性が高いです
私は本当に "歪み"を見ることができません。どのような歪みが発生しますか? – Ian
文書を定期的に印刷すると、より明確に表示されます。言葉に少し歪みがあります@lan – taji01
私はまだ画像の歪みを見ることができません?イメージを表示していないようですね。 – Ian