2016-11-10 45 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); 
     } 
    }