有很多类似的问题,但似乎没有一个能回答我的问题。
我正在开发一个 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 之前对其进行裁剪。 我在没有裁剪图表的情况下解决了这个问题,它工作正常,但裁剪它是更好更好的解决方案。
感谢您的帮助或建议!
最佳答案
此代码从现有的 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);
}
}
关于c# - 如何在 UWP 应用程序中裁剪位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40537189/