2017-02-16 67 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; 
}