简体   繁体   中英

rescale image before printing

I'm taking a screenshot of my form and then sending it to the printer. The image is too large, it goes off the sides of the page. I've been looking around for the past few hours to no avail. Can someone assist?

When I open the file itself it looks good in a print preview. If I then print from the preview its fine. But I wanted to do this with no user intervention.

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

So what I did just now as instead of the screen shot, for testing, I save what was in panel3 which is 2 pictureboxes. So i'm taking the size of panel3.

    Bitmap bmp = new Bitmap(panel3.ClientSize.Width, panel3.ClientSize.Height);
    panel3.DrawToBitmap(bmp, panel3.ClientRectangle);
    bmp.Save(subPath + file + ".bmp");

Which again, looks great on a print preview and if I click print from the print preview it prints fine. But if I just send it straight to the printer it doesn't fit. So maybe its not a size issue but a setting I have to send to the printer when not using print preview?

Update So I may have found the issue. When you un-check "Fit to frame" when printing a picture it fits perfectly. However there doesn't seem to be an option when sending directly to the printer the way I am where I can disable "Fit to frame"

Using this you can resize the image. You need to pick up a scaling size before print.

As example - rescale 'image' to 227x171 pixels.

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

If you want to resize and keep the aspect of the image I do the following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM