I took a picture using MediaCapture object and got a SoftwareBitmap. now I want to crop the image. how do I do that?
edit-this is what I tried so far:
public async Task<SoftwareBitmap> CorpSoftwareBitmap(SoftwareBitmap original ,float scale)
{
int startIndex = 54;
int M = original.PixelWidth;
int N = original.PixelHeight;
int m = (int)Math.Round(M * scale);
int n = (int)Math.Round(N * scale);
byte[] arrayOriginal;
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId,ms);
encoder.SetSoftwareBitmap(original);
await encoder.FlushAsync();
arrayOriginal = new byte[ms.Size];
await ms.ReadAsync(arrayOriginal.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
}
byte[] arrayCropped;
SoftwareBitmap cropped = new SoftwareBitmap(original.BitmapPixelFormat, m, n);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, ms);
encoder.SetSoftwareBitmap(cropped);
await encoder.FlushAsync();
arrayCropped = new byte[ms.Size];
await ms.ReadAsync(arrayCropped.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
}
int tmp = startIndex + 2 * (N - n) * M + 2 * (M - m);
for (int i=0; i<n;i++)
{
Array.ConstrainedCopy(arrayOriginal, tmp + 4 * M * i, arrayCropped,startIndex + m * i, m);
}
cropped.CopyFromBuffer(arrayCropped.AsBuffer());
//using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
//{
// ms.GetInputStreamAt(0);
// BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, ms);
// encoder.SetSoftwareBitmap(cropped);
// await encoder.FlushAsync();
// await ms.WriteAsync(arrayCropped.AsBuffer());
//}
return cropped;
}
and it doesn't write arrayCropped into cropped. both ways don't work, how do I fix that?
Thanks, Mor
ok, this one works:
public async Task<SoftwareBitmap> CorpSoftwareBitmap(SoftwareBitmap original, float scale=0.5f)
{
int startIndex = 54;//Bitmap header length
int M = original.PixelWidth;
int N = original.PixelHeight;
int m = (int)Math.Round(M * scale);
int n = (int)Math.Round(N * scale);
byte[] arrayOriginal;
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, ms);
encoder.SetSoftwareBitmap(original);
await encoder.FlushAsync();
arrayOriginal = new byte[ms.Size];
await ms.ReadAsync(arrayOriginal.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
}
byte[] arrayCropped;
SoftwareBitmap cropped = new SoftwareBitmap(original.BitmapPixelFormat, m, n);
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, ms);
encoder.SetSoftwareBitmap(cropped);
await encoder.FlushAsync();
arrayCropped = new byte[ms.Size];
await ms.ReadAsync(arrayCropped.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);
}
int tmp = startIndex + 2 * (N - n) * M + 2 * (M - m);
for (int i = 0; i < n; i++)
Array.ConstrainedCopy(arrayOriginal, tmp + 4 * M * i, arrayCropped,4 * m * (n-1-i), 4 * m);
cropped.Dispose();
cropped = SoftwareBitmap.CreateCopyFromBuffer(arrayCropped.AsBuffer(), BitmapPixelFormat.Bgra8, m, n);
return cropped;
}
There're some existing extend control to do it.
For example: An ImageCropper control for the Universal Windows Platform
The source code: https://github.com/XamlBrewer/UWP-ImageCropper-/tree/master/XamlBrewer.Uwp.Controls
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.