c# - How to crop bitmap in UWP app? -
there many similar questions, none of them seem answer problem.
i working on uwp app (with syncfusion), can generate pdf. pdf, need syncfusion.pdf.graphics.pdfbitmap, before that, need crop little bit.
this doing, diagram , transform pdfbitmap:
var diagrambitmap = new pdfbitmap(diagram);
then need draw pdf by:
document.pages[0].graphics.drawimage(diagrambitmap, x, y, width, height);
problem is, none of solutions found worked in uwp crop pdfbitmap before drawing it. solved problem without cropping diagram, it's working, crop more better , nicer solution.
thanks or advice!
problem is, none of solutions found worked in uwp crop pdfbitmap before drawing it. solved problem without cropping diagram, it's working, crop more better , nicer solution.
you can crop before pdfbitmap
creation.there sample project cropping images in uwp. please refer uwp-imagecropper. can modify core function below let meet requirement:
/// <param name="originalimgfile">image file want crop</param> /// <param name="startpoint">from point want crop</param> /// <param name="corpsize">the crop size of 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 , size 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 decoder stream. decoder, can // properties of image. bitmapdecoder decoder = await bitmapdecoder.createasync(stream); // scaledsize of original image. uint scaledwidth = (uint)math.floor(decoder.pixelwidth * scale); uint scaledheight = (uint)math.floor(decoder.pixelheight * scale); // refine start point , size. if (startpointx + width > scaledwidth) { startpointx = scaledwidth - width; } if (startpointy + height > scaledheight) { startpointy = scaledheight - height; } // create cropping bitmaptransform , define 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; // cropped pixels within bounds of transform. pixeldataprovider pix = await decoder.getpixeldataasync( bitmappixelformat.bgra8, bitmapalphamode.straight, transform, exiforientationmode.ignoreexiforientation, colormanagementmode.colormanagetosrgb); byte[] pixels = pix.detachpixeldata(); // stream bytes writeablebitmap writeablebitmap cropbmp = new writeablebitmap((int)width, (int)height); stream pixstream = cropbmp.pixelbuffer.asstream(); pixstream.write(pixels, 0, (int)(width * height * 4)); return pixstream; } }
then can use pdfbitmap
creation:
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);
Comments
Post a Comment