2016-11-10 34 views
0

有很多類似的問題,但他們都沒有回答我的問題。如何在UWP應用程序中裁剪位圖?

我正在開發一個UWP應用程序(帶有Syncfusion),它可以生成一些PDF。 對於這個PDF,我需要得到一些Syncfusion.Pdf.Graphics.PdfBitmap,但在此之前,我需要裁剪一下。

這是我在做什麼,我讓我的圖,並將其轉化爲PdfBitmap:

var diagramBitmap = new PdfBitmap(diagram); 

然後我需要把它畫成PDF:

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

的問題是,沒有我發現的解決方案很容易在UWP中繪製PdfBitmap。 我解決了這個問題,但沒有修剪圖表,它正在工作,但裁剪它是更好,更好的解決方案。

感謝您的幫助或建議!

回答

0

問題是,我發現在UWP中很難輕易在繪製PdfBitmap之前裁剪該解決方案。我解決了這個問題,但沒有修改圖表,它正在工作,但裁剪它是更好更好的解決方案。

您可以裁剪之前的PdfBitmap creation.There是在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); 
     } 
    }