2017-02-16 40 views
0

我正在截取表单并将其发送到打印机。图片太大,会偏离页面两侧。过去几个小时我一直在四处寻找,无济于事。有人可以协助吗?打印前重新调整图像

当我打开文件本身时,它在打印预览中看起来不错。如果我从预览中打印出来就好了。但我想在没有用户干预的情况下做到这一点。

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个pictureboxes。所以我采取了panel3的大小。

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

再一次,在打印预览中看起来不错,如果从打印预览中点击打印,打印效果会很好。但是,如果我直接将它发送到打印机,它不适合。所以也许它不是一个尺寸问题,而是一个设置,我不得不发送到打印机时,不使用打印预览?

更新 所以我可能已经找到了问题。打印图片时,如果取消选中“适合框架”,则完全适合。但是,直接发送到打印机的方式似乎没有选择我可以禁用“适合框架”的方式

+0

你可以计算出你想要的DPI /需求,然后将其设置在该位图。 – TaW

回答

0

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

举例 - 重新调整“图像”为227x171像素。

image = new Bitmap(image, new Size(227, 171)); 
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; 
}