2017-02-16 5 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(); 
    } 

だから私は今のように代わりのスクリーンショットだけで何をしたか、テストのために、私は2 pictureboxesあるパネル3にあったものを保存します。だから私はpanel3のサイズを取っています。

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

印刷プレビューではすばらしく、印刷プレビューから印刷をクリックすると印刷がうまくいきます。しかし、プリンタにまっすぐに送ると、それは合わない。サイズの問題ではなく、印刷プレビューを使用しないときにプリンタに送信する設定が必要なのでしょうか?

更新 この問題を発見した可能性があります。写真を印刷するときに「枠に合わせる」のチェックを外すと、それは完璧にフィットします。ただし、「フレームに合わせる」機能を無効にできる方法は、プリンタに直接送信するときには選択肢がないようです。

+0

次の操作を行い、画像の縦横のサイズを変更しておきたい場合。 – TaW

答えて

0

これを使用して画像のサイズを変更できます。プリントする前にスケーリングサイズを取得する必要があります。

例として、「画像」を227x171ピクセルに再スケールします。

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

あなたは私はあなたが欲しいDPI /必要性を計算して、ビットマップでそれを設定することができます

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; 
} 
関連する問題