嗨,我需要有关如何从SoftwareBitmap
C#UWP中获取字节数组的帮助,以便我可以通过TCP套接字发送它.
我还可以访问"VideoFrame previewFrame"对象,这是我从中获取的对象SoftwareBitmap
.
我在网上看到了类似下面的内容,但是UWP并不支持wb.SaveJpeg(...)
.除非我错过了什么?
MemoryStream ms = new MemoryStream(); WriteableBitmap wb = new WriteableBitmap(myimage); wb.SaveJpeg(ms, myimage.PixelWidth, myimage.PixelHeight, 0, 100); byte [] imageBytes = ms.ToArray();
任何帮助,或正确方向的指示,将不胜感激.
谢谢,安迪
当然,您可以从中访问编码 byte[]
数组SoftwareBitmap
。
参见此示例,提取jpeg编码byte[]
:
// includes BitmapEncoder, which defines some static encoder IDs using Windows.Graphics.Imaging; private async void PlayWithData(SoftwareBitmap softwareBitmap) { // get encoded jpeg bytes var data = await EncodedBytes(softwareBitmap, BitmapEncoder.JpegEncoderId); // todo: save the bytes to a DB, etc } private async TaskEncodedBytes(SoftwareBitmap soft, Guid encoderId) { byte[] array = null; // First: Use an encoder to copy from SoftwareBitmap to an in-mem stream (FlushAsync) // Next: Use ReadAsync on the in-mem stream to get byte[] array using (var ms = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, ms); encoder.SetSoftwareBitmap(soft); try { await encoder.FlushAsync(); } catch ( Exception ex ){ return new byte[0]; } array = new byte[ms.Size]; await ms.ReadAsync(array.AsBuffer(), (uint)ms.Size, InputStreamOptions.None); } return array; }
到目前为止我知道你不能这样做.但您可以使用SoftwareBitmap.看例子:https://msdn.microsoft.com/en-us/library/windows/apps/mt244351.aspx(SoftwareBitmap是SoftwareBitmapSource的私人领域..通过反射.刚读它......也许这是完全错误的建议)
private async void SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, StorageFile outputFile) { using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) { // Create an encoder with the desired format BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); // Set the software bitmap encoder.SetSoftwareBitmap(softwareBitmap); // Set additional encoding parameters, if needed encoder.BitmapTransform.ScaledWidth = 320; encoder.BitmapTransform.ScaledHeight = 240; encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees; encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant; encoder.IsThumbnailGenerated = true; try { await encoder.FlushAsync(); } catch (Exception err) { switch (err.HResult) { case unchecked((int)0x88982F81): //WINCODEC_ERR_UNSUPPORTEDOPERATION // If the encoder does not support writing a thumbnail, then try again // but disable thumbnail generation. encoder.IsThumbnailGenerated = false; break; default: throw; } } if (encoder.IsThumbnailGenerated == false) { await encoder.FlushAsync(); } } }
要从SoftwareBitmap获取字节数组,您可以使用"SoftwareBitmap.CopyToBuffer"
但是,首先你需要:
使用System.Runtime.InteropServices.WindowsRuntime;
因为方法AsBuffer()到byte []
...
StorageFile file = await StorageFile.GetFileFromPathAsync(ImageFilePath); using (IRandomAccessStream fileStream = await File.OpenAsync(FileAccessMode.Read), memStream = new InMemoryRandomAccessStream()) { // Open a Stream and decode a JPG image BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); var softwareBitmap = await decoder.GetSoftwareBitmapAsync(); byte [] imageBytes = new byte[4*decoder.PixelWidth*decoder.PixelHeight]; softwareBitmap.CopyToBuffer(imageBytes.AsBuffer()); //... now you can use the imageBytes[] }