C# 用c语言显示大图像#

C# 用c语言显示大图像#,c#,printing,resize,C#,Printing,Resize,我想用C#将一个大图像显示成一个小矩形。 问题是,当我不调整矩形大小和图像时,每次只显示图像的一半。是否有任何方法可以在定义的边界内完全打印图像?我的意思是,图像可以将自身大小重新调整为矩形大小吗 以下是我的代码: int imagePrintHeight = this.PaperSize.Width - this.PrintMargins.Top - this.PrintMargins.Bottom; int imagePrintWidth = this.PaperSize.Height -

我想用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));

如果我理解正确的话,您似乎正在尝试使图像适合可用的页面大小。在指定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);

答案有用吗?在这种情况下,请接受它,或者如果您仍然有问题,请提供更多信息。@ekholm提供的答案并不能解决我的问题。好吧,也许我当时误解了这个问题,或者有其他问题。如果您仍然需要帮助,请澄清您的问题所在。