C# 打印前重新缩放图像

C# 打印前重新缩放图像,c#,image,printing,C#,Image,Printing,我正在拍摄表格的截图,然后将其发送到打印机。图像太大,会从页面两侧脱落。在过去的几个小时里,我一直在四处寻找,但毫无结果。有人能帮忙吗 当我打开文件本身时,它在打印预览中看起来很好。如果我然后从预览打印它的罚款。但我想在没有用户干预的情况下实现这一点 public void SetupPrintHandler() { PrintDocument printDoc = new PrintDocument(); printDoc.PrintPage += ne

我正在拍摄表格的截图,然后将其发送到打印机。图像太大,会从页面两侧脱落。在过去的几个小时里,我一直在四处寻找,但毫无结果。有人能帮忙吗

当我打开文件本身时,它在打印预览中看起来很好。如果我然后从预览打印它的罚款。但我想在没有用户干预的情况下实现这一点

public void SetupPrintHandler()
    {
        PrintDocument printDoc = new PrintDocument();
        printDoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
        printDoc.DefaultPageSettings.Landscape = true;
        printDoc.Print();
    }

    private void OnPrintPage(object sender, PrintPageEventArgs args)
    {
        using (Image image = Image.FromFile(@"C:/temp2.bmp"))
        {
            Graphics g = args.Graphics;
            g.DrawImage(image, 0, 0);
        }
    }

    private void printscreen()
    {
        ScreenCapture sc = new ScreenCapture();
        Image img = sc.CaptureScreen();
        sc.CaptureWindowToFile(this.Handle, "C:/temp2.bmp", ImageFormat.Bmp);
        SetupPrintHandler();
    }
所以我刚才所做的不是屏幕截图,而是为了测试,我保存了panel3中的2个图片框。所以我选择了panel3的尺寸

    Bitmap bmp = new Bitmap(panel3.ClientSize.Width, panel3.ClientSize.Height);
    panel3.DrawToBitmap(bmp, panel3.ClientRectangle);
    bmp.Save(subPath + file + ".bmp");
同样,在打印预览上看起来很棒,如果我在打印预览中单击“打印”,打印效果会很好。但是如果我直接把它送到打印机上,它就不合适了。所以,也许这不是尺寸问题,而是在不使用打印预览时必须发送到打印机的设置

更新
所以我可能发现了这个问题。当您在打印图片时取消选中“适合帧”时,它会非常适合。但是,当直接向打印机发送时,似乎没有一个选项,我可以在那里禁用适合帧

使用此选项,您可以调整图像大小。在打印之前,您需要选择缩放尺寸

例如-将“图像”重新缩放到227x171像素

image = new Bitmap(image, new Size(227, 171));

如果您想调整大小并保持图像的外观,我将执行以下操作

 public Stream ResizeImage(Stream stream, ImageFormat imageFormat, int width, int height)
 {
    var originalImage = Image.FromStream(stream);

    var sourceWidth = originalImage.Width;
    var sourceHeight = originalImage.Height;
    const int sourceX = 0;
    const int sourceY = 0;
    var destX = 0;
    var destY = 0;

    float nPercent;

    var nPercentW = ((float)width / sourceWidth);
    var nPercentH = ((float)height / sourceHeight);

    if (nPercentH < nPercentW)
    {
        nPercent = nPercentH;
        destX = Convert.ToInt16((width - (sourceWidth * nPercent)) / 2);
    }
    else
    {
        nPercent = nPercentW;
        destY = Convert.ToInt16((height - (sourceHeight * nPercent)) / 2);
    }

    var destWidth = (int)(sourceWidth * nPercent);
    var destHeight = (int)(sourceHeight * nPercent);


    // specify different formats based off type ( GIF and PNG need alpha channel - 32aRGB  8bit Red  8bit Green  8bit B  8bit Alpha)
    Bitmap newImage;

    if (imageFormat.Equals(ImageFormat.Png) || imageFormat.Equals(ImageFormat.Gif))
    {
        newImage = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    }
    else
    {
        newImage = new Bitmap(width, height, PixelFormat.Format24bppRgb);
    }


    newImage.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);

    var newGraphics = Graphics.FromImage(newImage);

    // don't clear the buffer with white if we have transparency
    if (!imageFormat.Equals(ImageFormat.Png) && !imageFormat.Equals(ImageFormat.Gif))
    {
        newGraphics.Clear(Color.White);
    }


    newGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

    newGraphics.DrawImage(originalImage,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);

    newGraphics.Dispose();
    originalImage.Dispose();

    var memoryStream = new MemoryStream();
    newImage.Save(memoryStream, imageFormat);
    memoryStream.Position = 0;

    return memoryStream;
 }

您可以计算想要/需要的dpi,然后在位图中进行设置。