在c#中显示大图

时间:2012-06-05 07:36:00

标签: c# printing resize

我想使用C#将大图像显示为一个小矩形。 问题是,当我不调整矩形大小和图像时,每次只显示图像的一半。有没有办法在定义的边界内完全打印图像?我的意思是图像可以将自身重新调整为矩形大小吗?

以下是我的代码:

int imagePrintHeight = this.PaperSize.Width - this.PrintMargins.Top - this.PrintMargins.Bottom;
 int imagePrintWidth = this.PaperSize.Height - this.PrintMargins.Left - this.PrintMargins.Right;

Size DatestoPrintSize = TextRenderer.MeasureText(DatestoPrint, new Font(this.Font.FontFamily, 10));

                            y = y + descriptionSize.Height + DatestoPrintSize.Height;
                            imagePrintHeight = imagePrintHeight - descriptionSize.Height - DatestoPrintSize.Height;

                            e.Graphics.DrawString(objCurrentPrintJob.sDescription, new Font(this.Font.FontFamily, 10), new SolidBrush(Color.Black), x + (imagePrintWidth - descriptionSize.Width) / 2, this.PrintMargins.Top);
                            e.Graphics.DrawString(DatestoPrint, new Font(this.Font.FontFamily, 10), new SolidBrush(Color.Black), x + (imagePrintWidth - DatestoPrintSize.Width) / 2, this.PrintMargins.Top + descriptionSize.Height);
                        }

                        decimal ratio = Math.Round(decimal.Divide(imagetoPrint.Width, imagetoPrint.Height), 4);
                        int tempimagePrintWidth = (int)(Math.Round(imagePrintHeight * ratio, 4));
                        x += (int)((imagePrintWidth - tempimagePrintWidth) / 2);

要打印我使用以下

 e.Graphics.DrawImage(imagetoPrint, new Rectangle(x,y ,tempimagePrintWidth,imagePrintHeight));

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您似乎正在尝试使图像适合可用的页面大小。在分配imagePrintHeight和imagePrintWidth时,看起来混淆了宽度和高度。我会做这样的缩放,它还允许图像和页面宽高比之间的不同关系:

int imagePrintHeight = this.PaperSize.Height- this.PrintMargins.Top - this.PrintMargins.Bottom;
int imagePrintWidth = this.PaperSize.Width- this.PrintMargins.Left - this.PrintMargins.Right;

// Draw description string here...

double ratio = Math.Min((double)imagePrintWidth / (double)imagetoPrint.Width, (double)imagePrintHeight / (double)imagetoPrint.Height);
int scaledImageWidth = (int)(ratio * imageWidth);
int scaledImageHeight = (int)(ratio * imageHeight);

x = (int)((imagePrintWidth - scaledImageWidth ) / 2);