2016-11-10 8 views
0

似たような質問がたくさんありますが、いずれも私の問題に答えていないようです。UWPアプリケーションでビットマップをトリミングする方法は?

私はいくつかのPDFを生成することができるUWPアプリ(Syncfusion付き)に取り組んでいます。 このPDFには、私はSyncfusion.Pdf.Graphics.PdfBitmapを取得する必要がありますが、それ以前に少し切り抜く必要があります。

これは私がやっているものです、私は私の図を取得し、PdfBitmapに変換し:

var diagramBitmap = new PdfBitmap(diagram); 

その後、私はPDFにそれを描画する必要があることによって:

document.Pages[0].Graphics.DrawImage(diagramBitmap, x, y, width, height); 

問題は、のいずれでもありません私が見つけた解決策は、PdfBitmapを作図する前にUWPで簡単に作業していました。 私はダイアグラムを切り取ることなくその問題を解決しました。それは機能していますが、それを切り抜くことははるかに優れた、より良いソリューションです。

ご協力ありがとうございました!

答えて

0

問題は、私が見つけた解決策のどれも、UWPで描画する前にそのPdfBitmapをトリミングするのは簡単ではありませんでした。私は、ダイアグラムを切り取ることなくその問題を解決しました。それは機能していますが、それを切り抜くことは、はるかに優れた、より良いソリューションです。

PdfBitmapの作成前に切り抜くことができます.UWPで画像を切り抜くためのサンプルプロジェクトがあります。 UWP-ImageCropperを参照してください。あなたはそれがあなたの要件を満たすようにする以下のようなコア機能を変更することができます。

/// <param name="originalImgFile">Image File that you want to crop</param> 
/// <param name="startPoint">From which point you want to crop</param> 
/// <param name="corpSize">The crop size of the image</param> 
/// <param name="scale">The scale of cropped image</param> 
async public static Task<Stream> GetCroppedBitmapAsync(StorageFile originalImgFile, System.Drawing.Point startPoint, System.Drawing.Size corpSize, double scale) 
{ 
    if (double.IsNaN(scale) || double.IsInfinity(scale)) 
    { 
     scale = 1; 
    } 


    // Convert start point and size to integer. 
    uint startPointX = (uint)Math.Floor(startPoint.X * scale); 
    uint startPointY = (uint)Math.Floor(startPoint.Y * scale); 
    uint height = (uint)Math.Floor(corpSize.Height * scale); 
    uint width = (uint)Math.Floor(corpSize.Width * scale); 


    using (IRandomAccessStream stream = await originalImgFile.OpenReadAsync()) 
    { 


     // Create a decoder from the stream. With the decoder, we can get 
     // the properties of the image. 
     BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); 

     // The scaledSize of original image. 
     uint scaledWidth = (uint)Math.Floor(decoder.PixelWidth * scale); 
     uint scaledHeight = (uint)Math.Floor(decoder.PixelHeight * scale); 



     // Refine the start point and the size. 
     if (startPointX + width > scaledWidth) 
     { 
      startPointX = scaledWidth - width; 
     } 

     if (startPointY + height > scaledHeight) 
     { 
      startPointY = scaledHeight - height; 
     } 


     // Create cropping BitmapTransform and define the bounds. 
     BitmapTransform transform = new BitmapTransform(); 
     BitmapBounds bounds = new BitmapBounds(); 
     bounds.X = startPointX; 
     bounds.Y = startPointY; 
     bounds.Height = height; 
     bounds.Width = width; 
     transform.Bounds = bounds; 


     transform.ScaledWidth = 100; 
     transform.ScaledHeight = 100; 

     // Get the cropped pixels within the bounds of transform. 
     PixelDataProvider pix = await decoder.GetPixelDataAsync(
      BitmapPixelFormat.Bgra8, 
      BitmapAlphaMode.Straight, 
      transform, 
      ExifOrientationMode.IgnoreExifOrientation, 
      ColorManagementMode.ColorManageToSRgb); 
     byte[] pixels = pix.DetachPixelData(); 


     // Stream the bytes into a WriteableBitmap 
     WriteableBitmap cropBmp = new WriteableBitmap((int)width, (int)height); 
     Stream pixStream = cropBmp.PixelBuffer.AsStream(); 
     pixStream.Write(pixels, 0, (int)(width * height * 4)); 


     return pixStream; 
    } 
} 

次に、あなたがあなたのPdfBitmap創造のためにそれを使用することができます。

var imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/image03.jpg")); 
Stream bitmap = await GetCroppedBitmapAsync(imageFile, new System.Drawing.Point(100, 100), new System.Drawing.Size(100, 100), 1); 
PdfBitmap image = new PdfBitmap(bitmap); 
+1

トリミングするビットマップがすでにメモリにある場合(たとえばSoftwareBitmapなど)はどうでしょうか? –

0

このコードは、既存のSoftwareBitmapから新しいトリミングされたSoftwareBitmapを作成し、 。イメージがすでにメモリ内にある場合に便利です。

async public static Task<SoftwareBitmap> GetCroppedBitmapAsync(SoftwareBitmap softwareBitmap, 
      uint startPointX, uint startPointY, uint width, uint height) 
    {    
     using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) 
     { 
      BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream); 

      encoder.SetSoftwareBitmap(softwareBitmap); 

      encoder.BitmapTransform.Bounds = new BitmapBounds() 
      { 
       X = startPointX, 
       Y = startPointY, 
       Height = height, 
       Width = width 
      }; 

      await encoder.FlushAsync(); 

      BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); 

      return await decoder.GetSoftwareBitmapAsync(softwareBitmap.BitmapPixelFormat, softwareBitmap.BitmapAlphaMode); 
     } 
    } 
関連する問題