打印前重新缩放图像

时间:2017-02-16 13:19:49

标签: c# image printing

我正在截取我的表单截图,然后将其发送到打印机。图像太大,它会从页面的两侧出现。我一直在寻找过去几个小时无济于事。有人可以帮忙吗?

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

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");

再次,在打印预览上看起来很棒,如果我从打印预览中单击打印它打印正常。但是,如果我直接将它发送到打印机,它就不适合。那么也许它不是尺寸问题,而是我不得不在不使用打印预览时发送到打印机的设置?

更新 所以我可能已经找到了这个问题。当您在打印图片时取消选中“适合框架”时,它非常适合。但是,当我直接发送到打印机时,似乎没有选项可以禁用“Fit to frame”

2 个答案:

答案 0 :(得分:0)

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

例如 - 将'image'重新缩放为227x171像素。

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

答案 1 :(得分:0)

如果您想调整大小并保留图像的方面,请执行以下操作

 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;
 }