public override async Task <object?> ByteToBitmap(byte[]?bytes, int decodeWidth = 0, int decodeHeight = 0, bool transcode = false, object?img = default) { if (bytes == null) { return(null); } if (bytes.Length == 0) { return(null); } var image = img as BitmapImage ?? new BitmapImage(); image.DecodePixelType = DecodePixelType.Logical; if (decodeWidth > 0) { image.DecodePixelWidth = decodeWidth; } if (decodeHeight > 0) { image.DecodePixelHeight = decodeHeight; } using (var stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(bytes.AsBuffer()); stream.Seek(0); if (transcode) { await semaphore.WaitAsync(); try { SoftwareBitmap softwareBitmap; var decoder = await BitmapDecoder.CreateAsync(stream); using (softwareBitmap = await decoder.GetSoftwareBitmapAsync()) { if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight) { softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } using (var converted = new InMemoryRandomAccessStream()) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, converted); encoder.SetSoftwareBitmap(softwareBitmap); await encoder.FlushAsync(); await image.SetSourceAsync(converted); } } } catch (Exception) { return(null); } finally { semaphore.Release(); } } else { try { await(image as BitmapImage)?.SetSourceAsync(stream); } catch (Exception) { return(null); } } } return(image); }
private async Task DetectEmmotion() { try { var stream = new InMemoryRandomAccessStream(); await _mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); var memoryStream = await ConvertFromInMemoryRandomAccessStream(stream); Emotion[] result = await _client.RecognizeAsync(memoryStream); if (result.Any()) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Hiding all score smileys foreach (var scoreGridChild in ScoreGrid.Children) { scoreGridChild.Opacity = 0; } double scoreHappy = (result.Sum(a => a.Scores.Happiness) / result.Length); double scoreAnger = (result.Sum(a => a.Scores.Anger) / result.Length) * 2; // Happy if (scoreHappy > scoreAnger) { if (scoreHappy > 0.5) { Score5.Opacity = 1; } else { Score4.Opacity = 1; } } // Angry if (scoreHappy < scoreAnger) { if (scoreAnger > 0.5) { Score1.Opacity = 1; } else { Score2.Opacity = 1; } } // Neutral if (scoreHappy < 0.2 && scoreAnger < 0.2) { Score1.Opacity = 0; Score2.Opacity = 0; Score3.Opacity = 0; Score4.Opacity = 0; Score5.Opacity = 0; Score3.Opacity = 1; } } ); } } catch (Exception ex) { Debug.WriteLine("Exception when detection emotion: {0}", ex.Message); } }
private async Task <bool> TakePictureAsync() { bool successful = true; if (_state != StreamingState.Streaming) { return(false); } try { IList <DetectedFace> faces; const BitmapPixelFormat PixelFormat = BitmapPixelFormat.Nv12; using (VideoFrame currentFrame = new VideoFrame(PixelFormat, (int)_properties.Width, (int)_properties.Height)) { await _mediaCapture.GetPreviewFrameAsync(currentFrame); faces = await _faceDetector.DetectFacesAsync(currentFrame.SoftwareBitmap); Size size = new Size(currentFrame.SoftwareBitmap.PixelWidth, currentFrame.SoftwareBitmap.PixelHeight); if (faces.Count == 0 || faces.Count > 1) { throw new Exception("Too many people. (Or no one.)"); } using (SoftwareBitmap bitmap = SoftwareBitmap.Convert(currentFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8)) { WriteableBitmap source = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(source.PixelBuffer); IRandomAccessStream stream = new InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); encoder.SetSoftwareBitmap(bitmap); await encoder.FlushAsync(); ShowUp(size, faces, source); if (UserName.Text.Equals(string.Empty)) { throw new Exception("Enter your name."); } if (await _faceApiHelper.CreatePersonAsync(stream.AsStream(), UserName.Text)) { ShowAlertHelper.ShowDialog("Hi " + UserName.Text + ".", "Success"); } else { ShowAlertHelper.ShowDialog("Something went wrong. Try again."); } } } } catch (Exception ex) { ShowAlertHelper.ShowDialog(ex.Message); successful = false; } return(successful); }
private async Task Initialize(FileSystemStorageFile PdfFile) { LoadingControl.IsLoading = true; PdfCollection.Clear(); LoadQueue.Clear(); Cancellation = new CancellationTokenSource(); MaxLoad = 0; LastPageIndex = 0; try { using (IRandomAccessStream PdfStream = await PdfFile.GetRandomAccessStreamFromFileAsync(FileAccessMode.Read)) { try { Pdf = await PdfDocument.LoadFromStreamAsync(PdfStream); } catch (Exception) { PdfPasswordDialog Dialog = new PdfPasswordDialog(); if ((await Dialog.ShowAsync()) == ContentDialogResult.Primary) { Pdf = await PdfDocument.LoadFromStreamAsync(PdfStream, Dialog.Password); } else { Frame.GoBack(); return; } } } for (uint i = 0; i < 10 && i < Pdf.PageCount && !Cancellation.IsCancellationRequested; i++) { using (PdfPage Page = Pdf.GetPage(i)) using (InMemoryRandomAccessStream PageStream = new InMemoryRandomAccessStream()) { await Page.RenderToStreamAsync(PageStream, new PdfPageRenderOptions { DestinationHeight = Convert.ToUInt32(Page.Size.Height * 1.5), DestinationWidth = Convert.ToUInt32(Page.Size.Width * 1.5) }); BitmapImage DisplayImage = new BitmapImage(); PdfCollection.Add(DisplayImage); await DisplayImage.SetSourceAsync(PageStream); } } } catch (Exception) { QueueContentDialog Dialog = new QueueContentDialog { Title = Globalization.GetString("Common_Dialog_ErrorTitle"), Content = Globalization.GetString("QueueDialog_PDFOpenFailure"), CloseButtonText = Globalization.GetString("Common_Dialog_GoBack") }; _ = await Dialog.ShowAsync(); Frame.GoBack(); } finally { if (!Cancellation.IsCancellationRequested) { Flip.SelectionChanged += Flip_SelectionChanged; Flip.SelectionChanged += Flip_SelectionChanged1; } await Task.Delay(1000); LoadingControl.IsLoading = false; } }
private void ProcessFrames() { _stoppedThreads--; while (_stopThreads == false) { try { GarbageCollectorCanWorkHere(); var frame = _mediaFrameReader.TryAcquireLatestFrame(); var frameDuration = new Stopwatch(); frameDuration.Start(); if (frame == null || frame.VideoMediaFrame == null || frame.VideoMediaFrame.SoftwareBitmap == null) { continue; } using (var stream = new InMemoryRandomAccessStream()) { using (var bitmap = SoftwareBitmap.Convert(frame.VideoMediaFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore)) { // var imageTask = BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream).AsTask(); var imageTask = BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, _imageQuality).AsTask(); imageTask.Wait(); SoftwareBitmap bitmapBuffer = SoftwareBitmap.Convert( frame.VideoMediaFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); Bitmap = bitmapBuffer; var encoder = imageTask.Result; //encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise270Degrees; encoder.SetSoftwareBitmap(bitmap); ////Rotate image 180 degrees //var transform = encoder.BitmapTransform; //transform.Rotation = BitmapRotation.Clockwise180Degrees; var flushTask = encoder.FlushAsync().AsTask(); flushTask.Wait(); using (var asStream = stream.AsStream()) { asStream.Position = 0; var image = new byte[asStream.Length]; asStream.Read(image, 0, image.Length); lock (_lastFrameAddedLock) { if (_lastFrameAdded.Elapsed.Subtract(frameDuration.Elapsed) > TimeSpan.Zero) { Frame = image; _lastFrameAdded = frameDuration; } } encoder = null; } } } } catch (ObjectDisposedException) { } } _stoppedThreads++; }
private async Task <InMemoryRandomAccessStream> ResizeImage(InMemoryRandomAccessStream stream, Size requiredSize) { // Make a decoder for the current stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); uint imageHeight = decoder.PixelHeight; uint imageWidth = decoder.PixelWidth; double widthRatio = imageWidth / requiredSize.Width; double heightRatio = imageHeight / requiredSize.Height; uint outputHeight = imageHeight; uint outputWidth = imageWidth; bool centerOnX = false; if (widthRatio < heightRatio) { outputHeight = (uint)(imageHeight / widthRatio); outputWidth = (uint)requiredSize.Width; centerOnX = false; } else { outputWidth = (uint)(imageWidth / heightRatio); outputHeight = (uint)requiredSize.Height; centerOnX = true; } // Make an output stream and an encoder InMemoryRandomAccessStream outputStream = new InMemoryRandomAccessStream(); BitmapEncoder enc = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder); // convert the entire bitmap to a 125px by 125px bitmap enc.BitmapTransform.ScaledHeight = outputHeight; enc.BitmapTransform.ScaledWidth = outputWidth; BitmapBounds bound = new BitmapBounds(); bound.Height = (uint)requiredSize.Height; bound.Width = (uint)requiredSize.Width; // Choose Fant for quality over perf. enc.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant; if (centerOnX) { int width = ((int)outputWidth / 2) - ((int)bound.Width / 2); bound.X = (uint)(width > 0 ? width : 0); } else { int height = ((int)outputHeight / 2) - ((int)bound.Height / 2); bound.Y = (uint)(height > 0 ? height : 0); } enc.BitmapTransform.Bounds = bound; // Do it await enc.FlushAsync(); // Return the new stream return(outputStream); }
private async Task DecodeLiveviewFrame(JpegPacket packet, bool retry = false) { Action trailingTask = null; if (LiveviewImageBitmap == null || sizeChanged) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { var writeable = await LiveviewUtil.AsWriteableBitmap(packet.ImageData, Dispatcher); OriginalLvSize = new BitmapSize { Width = (uint)writeable.PixelWidth, Height = (uint)writeable.PixelHeight }; var magnification = CalcLiveviewMagnification(); DebugUtil.Log(() => { return("Decode: mag: " + magnification + " offsetV: " + LvOffsetV); }); dpi = DEFAULT_DPI / magnification; RefreshOverlayControlParams(magnification); trailingTask = () => { if (Context?.Target?.Status != null) { RotateLiveviewImage(Context.Target.Status.LiveviewOrientationAsDouble, (sender, arg) => { RefreshOverlayControlParams(magnification); }); } sizeChanged = false; }; }); } else { rwLock.EnterWriteLock(); try { var toDelete = LiveviewImageBitmap; trailingTask = () => { // Dispose after it is drawn toDelete?.Dispose(); }; } finally { rwLock.ExitWriteLock(); } } using (var stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(packet.ImageData.AsBuffer()); stream.Seek(0); var bmp = await CanvasBitmap.LoadAsync(LiveviewImageCanvas, stream, (float)dpi); var size = bmp.SizeInPixels; rwLock.EnterWriteLock(); try { LiveviewImageBitmap = bmp; } finally { rwLock.ExitWriteLock(); } if (!OriginalLvSize.Equals(size)) { DisposeLiveviewImageBitmap(); if (!retry) { await DecodeLiveviewFrame(packet, true); } return; } } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { LiveviewImageCanvas.Invalidate(); trailingTask?.Invoke(); }); if (PendingPakcet != null) { var task = DecodeLiveviewFrame(PendingPakcet); PendingPakcet = null; } }
public static async void SaveAsync(MapperGame map, StorageFile zipFile) { CerasSerializer serializer = new CerasSerializer(GetConfig()); MapState state = new MapState() { Pixels = map.Pixels, Nations = map.Nations, Sides = map.Sides, Fronts = new Dictionary <UnorderedBytePair, sbyte>(map.Fronts), DialogText = map.DialogText, DialogRectangle = map.DialogRectangle, IsTreatyMode = map.IsTreatyMode }; using (Stream fileStream = await zipFile.OpenStreamForWriteAsync()) { using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Update)) { for (int i = zipArchive.Entries.Count - 1; i >= 0; i--) { zipArchive.Entries[i].Delete(); } ZipArchiveEntry entry = zipArchive.CreateEntry("map.png"); using (Stream pngStream = entry.Open()) { using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms); Stream pixelStream = map.baseImage.PixelBuffer.AsStream(); byte[] Pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(Pixels, 0, Pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)map.baseImage.PixelWidth, (uint)map.baseImage.PixelHeight, 96.0, 96.0, Pixels); await encoder.FlushAsync(); IBuffer data = (new byte[ms.Size]).AsBuffer(); ms.Seek(0); await ms.ReadAsync(data, (uint)ms.Size, InputStreamOptions.None); pngStream.SetLength(data.ToArray().Length); await pngStream.WriteAsync(data.ToArray(), 0, (int)pngStream.Length); await pngStream.FlushAsync(); } } entry = zipArchive.CreateEntry("state.json"); using (Stream stateStream = entry.Open()) { byte[] data = Encoding.Unicode.GetBytes(await Json.StringifyAsync(state)); stateStream.SetLength(data.Length); await stateStream.WriteAsync(data, 0, data.Length); await stateStream.FlushAsync(); } entry = zipArchive.CreateEntry("pixels.bin"); using (Stream stateStream = entry.Open()) { byte[] data = (new CerasSerializer(GetConfig())).Serialize(state.Pixels); stateStream.SetLength(data.Length); await stateStream.WriteAsync(data, 0, data.Length); await stateStream.FlushAsync(); } } } }
/// <summary> /// Reloads (or loads) the image with a decode size that is scaled to the /// screen. Unless useFulsize is set. /// </summary> /// <param name="useFullSize"></param> public bool ReloadImage(bool useFullsize) { // Don't worry about this if we are destroyed. if (m_base.IsDestoryed) { return(false); } // If we are already this size don't do anything. if (m_lastImageSetSize.Equals(m_currentControlSize) && !useFullsize) { return(false); } // If we don't have a control size yet don't do anything. if (m_currentControlSize.IsEmpty) { return(false); } // Don't do anything if we are already full size. if (useFullsize && m_lastImageSetSize.Width == 0 && m_lastImageSetSize.Height == 0) { return(false); } // Get the stream. InMemoryRandomAccessStream stream = m_imageSourceStream; if (stream == null) { return(false); } // Set our last size. if (useFullsize) { // Set our current size. m_lastImageSetSize.Width = 0; m_lastImageSetSize.Height = 0; } else { // Set our current size. m_lastImageSetSize.Width = m_currentControlSize.Width; m_lastImageSetSize.Height = m_currentControlSize.Height; } // Create a bitmap and BitmapImage bitmapImage = new BitmapImage(); bitmapImage.CreateOptions = BitmapCreateOptions.None; bitmapImage.ImageOpened += BitmapImage_ImageOpened; bitmapImage.ImageFailed += BitmapImage_ImageFailed; // Set the source. stream.Seek(0); bitmapImage.SetSource(stream); // Get the decode height and width. int decodeWidth = 0; int decodeHeight = 0; if (!useFullsize) { double widthRatio = bitmapImage.PixelWidth / m_currentControlSize.Width; double heightRatio = bitmapImage.PixelHeight / m_currentControlSize.Height; if (widthRatio > heightRatio) { decodeWidth = (int)m_currentControlSize.Width; } else { decodeHeight = (int)m_currentControlSize.Height; } } // Set the decode size. bitmapImage.DecodePixelHeight = decodeHeight; bitmapImage.DecodePixelWidth = decodeWidth; // If we have memory to play with use logical pixels for decode. This will cause the image to decode to the // logical size, meaning the physical pixels will actually be the screen resolution. If we choose physical pixels // the image will decode to the width of the control in physical pixels, so the image size to control size will be one to one. // But since this is scaled, the control size is something like 3 physical pixels for each logical pixel, so the image is lower res // if we use physical. bitmapImage.DecodePixelType = App.BaconMan.MemoryMan.MemoryPressure < MemoryPressureStates.Medium ? DecodePixelType.Logical : DecodePixelType.Physical; // Destroy the old image. if (m_image.Source != null) { BitmapImage currentImage = (BitmapImage)m_image.Source; if (currentImage != null) { // Remove the handlers currentImage.ImageOpened -= BitmapImage_ImageOpened; currentImage.ImageFailed -= BitmapImage_ImageFailed; } } // Set the image. m_image.Source = bitmapImage; return(true); }
private async Task <byte[]> GetImageAsByteAsync(Guid format, int quality, int desiredWidth, int desiredHeight) { if (Control == null || Control.Source == null) { return(null); } var bitmap = Control.Source as WriteableBitmap; if (bitmap == null) { return(null); } byte[] pixels = null; uint pixelsWidth = (uint)bitmap.PixelWidth; uint pixelsHeight = (uint)bitmap.PixelHeight; if (desiredWidth != 0 || desiredHeight != 0) { double widthRatio = (double)desiredWidth / (double)bitmap.PixelWidth; double heightRatio = (double)desiredHeight / (double)bitmap.PixelHeight; double scaleRatio = Math.Min(widthRatio, heightRatio); if (desiredWidth == 0) { scaleRatio = heightRatio; } if (desiredHeight == 0) { scaleRatio = widthRatio; } uint aspectWidth = (uint)((double)bitmap.PixelWidth * scaleRatio); uint aspectHeight = (uint)((double)bitmap.PixelHeight * scaleRatio); using (var tempStream = new InMemoryRandomAccessStream()) { byte[] tempPixels = await GetBytesFromBitmapAsync(bitmap); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, tempStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, pixelsWidth, pixelsHeight, 96, 96, tempPixels); await encoder.FlushAsync(); tempStream.Seek(0); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(tempStream); BitmapTransform transform = new BitmapTransform() { ScaledWidth = aspectWidth, ScaledHeight = aspectHeight, InterpolationMode = BitmapInterpolationMode.Linear }; PixelDataProvider pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); pixels = pixelData.DetachPixelData(); pixelsWidth = aspectWidth; pixelsHeight = aspectHeight; } } else { pixels = await GetBytesFromBitmapAsync(bitmap); } using (var stream = new InMemoryRandomAccessStream()) { var encoder = await BitmapEncoder.CreateAsync(format, stream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, pixelsWidth, pixelsHeight, 96, 96, pixels); await encoder.FlushAsync(); stream.Seek(0); var bytes = new byte[stream.Size]; await stream.ReadAsync(bytes.AsBuffer(), (uint)stream.Size, InputStreamOptions.None); return(bytes); } }
private async void MediaFrameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args) { var mediaFrameReference = sender.TryAcquireLatestFrame(); if (mediaFrameReference != null) { SoftwareBitmap openCVInputBitmap = null; var inputBitmap = mediaFrameReference.VideoMediaFrame?.SoftwareBitmap; if (inputBitmap != null) { //The XAML Image control can only display images in BRGA8 format with premultiplied or no alpha if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8 && inputBitmap.BitmapAlphaMode == BitmapAlphaMode.Premultiplied) { openCVInputBitmap = SoftwareBitmap.Copy(inputBitmap); } else { openCVInputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } SoftwareBitmap openCVOutputBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, openCVInputBitmap.PixelWidth, openCVInputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); if (frameSkip == 0) { frameSkip++; using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) { BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); // Set the software bitmap encoder.SetSoftwareBitmap(inputBitmap); await encoder.FlushAsync(); var bytes = new byte[stream.Size]; await stream.ReadAsync(bytes.AsBuffer(), (uint)stream.Size, InputStreamOptions.None); var client = new HttpClient(); var queryString = HttpUtility.ParseQueryString(string.Empty); // Request headers client.DefaultRequestHeaders.Add("Prediction-Key", ""); client.DefaultRequestHeaders.Add("Prediction-key", "3ac99d01a5ed4ea7a155b8fdc688a5fa"); var uri = "https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/0c1a773e-bb3d-4c84-9749-99bee73cbe1e/image?" + queryString; HttpResponseMessage response; using (ByteArrayContent streamContent = new ByteArrayContent(bytes)) { try { response = await client.PostAsync(uri, streamContent); if (response.IsSuccessStatusCode) { try { var content = await response.Content.ReadAsStringAsync(); HttpClient client2 = new HttpClient(); Item item = new Item() { id = 1, json = content }; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://luckycharms.azurewebsites.net/items"); request.Content = new StringContent(JsonConvert.SerializeObject(item), Encoding.UTF8, "application/json"); var response2 = await client2.SendAsync(request); if (!response2.IsSuccessStatusCode) { Debug.WriteLine(response2.ReasonPhrase); } await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { textElement.Text = content; }); } catch (Exception ex) { } } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } } } //using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream()) //{ // BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); // // Set the software bitmap // encoder.SetSoftwareBitmap(openCVOutputBitmap); // await encoder.FlushAsync(); // var predictionEndpoint = new PredictionEndpoint() { ApiKey = "3ac99d01a5ed4ea7a155b8fdc688a5fa" }; // var result = await predictionEndpoint.PredictImageWithHttpMessagesAsync(new Guid("0c1a773e-bb3d-4c84-9749-99bee73cbe1e"), stream.AsStream()); // await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => // { // textElement.Text = JsonConvert.SerializeObject(result.Body); // }); //} } else { frameSkip++; if (frameSkip > 20) { frameSkip = 0; } } // operate on the image and render it //openCVHelper.Blur(openCVInputBitmap, openCVOutputBitmap); _frameRenderer.PresentSoftwareBitmap(openCVInputBitmap); } } }
/// <summary> /// //Encoding a Win2D surface (CanvasRenderTarget) as a video frame /// </summary> private async void OnMSSSampleRequested(MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args) { if (parent != null) { parent.StartWritingOutput("OnSampleRequested " + frameCounter.ToString(), 0); } if (unpackList == null) { if (parent != null) { parent.StartWritingOutput("Unpack List Null Error!", 1); } //this will stop the encoding args.Request.Sample = null; return; } int len = unpackList.Count; if (frameCounter >= len) { if (parent != null) { parent.StartWritingOutput("Encoding Completed.", 1); } //this will stop the encoding args.Request.Sample = null; return; } if ((frameCounter < 0) || (0 == len)) { if (parent != null) { parent.StartWritingOutput("Invalid Frame", 1); } //this will stop the encoding args.Request.Sample = null; return; } //need deferral because CanvasBitmap.LoadAsync takes some time to complete ? var deferral = args.Request.GetDeferral(); /// UnpackItem unpackItem = unpackList[frameCounter]; Windows.Storage.Streams.Buffer buffer = unpackItem.compressedBuffer; InMemoryRandomAccessStream inMemoryRandomAccessStream = null; using (inMemoryRandomAccessStream = new InMemoryRandomAccessStream()) { await inMemoryRandomAccessStream.WriteAsync(buffer); await inMemoryRandomAccessStream.FlushAsync(); inMemoryRandomAccessStream.Seek(0); CanvasBitmap tempBitmap = null; try { tempBitmap = await CanvasBitmap.LoadAsync(CanvasDevice.GetSharedDevice(), inMemoryRandomAccessStream); } catch (Exception e) { if (parent != null) { parent.StartWritingOutput("CBM Error : " + e.Message, 1); } } if (tempBitmap != null) { CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), tempBitmap.SizeInPixels.Width, tempBitmap.SizeInPixels.Height, tempBitmap.Dpi); using (CanvasDrawingSession session = canvasRenderTarget.CreateDrawingSession()) { session.Clear(Colors.Black); //session.DrawEllipse(new System.Numerics.Vector2(120 + frameCounter * 2, 100), 30, 20, Colors.White); session.DrawImage(tempBitmap); } TimeSpan timeLapsed = unpackItem.frameTime; Timestamp += timeLapsed; //set sample after defferal ? nope ....stop at 1st frame... MediaStreamSample sample = MediaStreamSample.CreateFromDirect3D11Surface(canvasRenderTarget, Timestamp); args.Request.Sample = sample; //Clearing memory buffer for this frame //Crash //unpackList[frameCounter].compressedBuffer = null; //unpackItem.compressedBuffer = null; deferral.Complete(); } else { args.Request.Sample = null; deferral.Complete(); } frameCounter++; } }
public async void SeparateThreadToSaveVideoStream() { while (isRecording == true) { //thread is counting //sometimes stuck at thread at frame : 0, depending on the window chosen to record //meaning OnFrameArrived is not called if (parent != null) { DateTime currentTimeLocal = DateTime.Now; TimeSpan elpasedTimeLocal = currentTimeLocal - initialRecordTime; string debugstr = "At frame: " + counter.ToString() + " Threadcounter: " + threadcounter.ToString(); //debugstr += " StreamSize: " + ((int)(videostream.Size / 1024.0)).ToString() + "KB TimeElapsed: " + ((int)elpasedTimeLocal.TotalSeconds).ToString(); debugstr += " StreamSize: " + ((int)(totalMemoryUsed / 1024.0)).ToString() + " KB"; debugstr += " TimeElapsed: " + ((int)elpasedTimeLocal.TotalSeconds).ToString(); parent.msg(debugstr); } threadcounter++; if (threadcounter > 200000) { threadcounter = 0; } if (currentFrame != null) { CanvasBitmap canvasBitmap = CanvasBitmap.CreateFromDirect3D11Surface( canvasDevice, currentFrame.Surface); using (var inputstream = new InMemoryRandomAccessStream()) { CancellationToken ct = new CancellationToken(); await canvasBitmap.SaveAsync(inputstream, CanvasBitmapFileFormat.Png, 1f).AsTask(ct); ulong currentFrameLength = inputstream.Size; _currentVideoStreamPos = 0; totalMemoryUsed += currentFrameLength; DateTime currentTimeLocal = DateTime.Now; TimeSpan diff = currentTimeLocal - previousRecordTime; previousRecordTime = currentTimeLocal; UnpackItem unpackItem = new UnpackItem(); unpackItem.pos = _currentVideoStreamPos; unpackItem.length = currentFrameLength; unpackItem.frameTime = diff; unpackItem.compressedBuffer = new Windows.Storage.Streams.Buffer((uint)inputstream.Size); inputstream.Seek(0); await inputstream.ReadAsync(unpackItem.compressedBuffer, (uint)inputstream.Size, InputStreamOptions.None); //read from stream to buffer await inputstream.FlushAsync(); unpackList.Add(unpackItem); } currentFrame?.Dispose(); currentFrame = null; //need this line so this thread will continue loop when new frame is not yet ready } else { Thread.Sleep(10); } } //await CloseVideoStream(); Thread.Sleep(50); int len = unpackList.Count; DateTime currentTime = DateTime.Now; TimeSpan elpasedTime = currentTime - initialRecordTime; string debugstrx = "Num frame: " + len.ToString() + " Threadcounter: " + threadcounter.ToString(); debugstrx += " TimeElapsed: " + ((int)elpasedTime.TotalSeconds).ToString(); if (elpasedTime.TotalSeconds > 0) { debugstrx += " Frame Rate (fps) : " + (len / (double)elpasedTime.TotalSeconds).ToString(); } if (parent != null) { parent.StartWritingReport(debugstrx); } //await UnpackVideoStream(); }
public async void Execute(NativeViewHierarchyManager nvhm) { var view = nvhm.ResolveView(tag) as FrameworkElement; if (view == null) { promise.Reject(ErrorUnableToSnapshot, "No view found with reactTag: " + tag); return; } try { if ("file" == result) { using (var ras = new InMemoryRandomAccessStream()) { await CaptureView(view, ras); var file = await GetStorageFile(); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { await RandomAccessStream.CopyAndCloseAsync(ras.GetInputStreamAt(0), fileStream.GetOutputStreamAt(0)); promise.Resolve(file.Path); } } } else if ("base64" == result) { using (var ras = new InMemoryRandomAccessStream()) { await CaptureView(view, ras); var imageBytes = new byte[ras.Size]; await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length); string data = Convert.ToBase64String(imageBytes); promise.Resolve(data); } } else if ("data-uri" == result) { using (var ras = new InMemoryRandomAccessStream()) { await CaptureView(view, ras); var imageBytes = new byte[ras.Size]; await ras.AsStream().ReadAsync(imageBytes, 0, imageBytes.Length); string data = Convert.ToBase64String(imageBytes); data = "data:image/" + extension + ";base64," + data; promise.Resolve(data); } } else { promise.Reject(ErrorUnableToSnapshot, "Unsupported result: " + result + ". Try one of: file | base64 | data-uri"); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); promise.Reject(ErrorUnableToSnapshot, "Failed to capture view snapshot"); } }
/// <summary> /// Converts the specified byte array to a <see cref="IRandomAccessStream" />. /// </summary> /// <param name="buffer"></param> /// <returns></returns> private static async Task<IRandomAccessStream> ConvertToRandomAccessStream(byte[] buffer) { //https://stackoverflow.com/questions/16397509/how-to-convert-byte-array-to-inmemoryrandomaccessstream-or-irandomaccessstream-i var randomAccessStream = new InMemoryRandomAccessStream(); await randomAccessStream.WriteAsync(buffer.AsBuffer()); randomAccessStream.Seek(0); return randomAccessStream; }
public static async Task <MapperGame> LoadAsync(StorageFile zipFile) { CerasSerializer serializer = new CerasSerializer(GetConfig()); if (zipFile == null) { return(null); } WriteableBitmap bmp; MapState state; using (Stream fileStream = await zipFile.OpenStreamForReadAsync()) { using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read)) { ZipArchiveEntry entry = zipArchive.GetEntry("map.png"); if (entry == null) { return(null); } using (Stream pngStream = entry.Open()) { using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) { byte[] readBuffer = new byte[4096]; int bytesRead; while ((bytesRead = pngStream.Read(readBuffer, 0, 4096)) > 0) { byte[] b = new byte[bytesRead]; Array.Copy(readBuffer, b, bytesRead); await ms.WriteAsync(b.AsBuffer()); } BitmapDecoder decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, ms); using (SoftwareBitmap swbmp = await decoder.GetSoftwareBitmapAsync()) { bmp = new WriteableBitmap(swbmp.PixelWidth, swbmp.PixelHeight); swbmp.CopyToBuffer(bmp.PixelBuffer); } } } entry = zipArchive.GetEntry("state.json"); if (entry == null) { return(null); } using (Stream stateStream = entry.Open()) { byte[] readBuffer = new byte[4096]; int bytesRead; MemoryStream ms = new MemoryStream(); while ((bytesRead = stateStream.Read(readBuffer, 0, 4096)) > 0) { byte[] b = new byte[bytesRead]; Array.Copy(readBuffer, b, bytesRead); await ms.WriteAsync(b, 0, bytesRead); } state = await Json.ToObjectAsync <MapState>(Encoding.Unicode.GetString(ms.ToArray())); } entry = zipArchive.GetEntry("pixels.bin"); if (entry == null) { return(null); } using (Stream stateStream = entry.Open()) { byte[] readBuffer = new byte[4096]; int bytesRead; MemoryStream ms = new MemoryStream(); while ((bytesRead = stateStream.Read(readBuffer, 0, 4096)) > 0) { byte[] b = new byte[bytesRead]; Array.Copy(readBuffer, b, bytesRead); await ms.WriteAsync(b, 0, bytesRead); } state.Pixels = (new CerasSerializer(GetConfig())).Deserialize <PixelData[, ]>(ms.ToArray()); } } MapperGame map = new MapperGame(bmp); map.Nations = state.Nations; map.Sides = state.Sides; map.Pixels = state.Pixels; map.Fronts = state.Fronts; map.DialogText = state.DialogText; map.DialogRectangle = state.DialogRectangle; return(map); } }
public async Task <bool> ApplyLogo() { var result = false; SetupInitialLogo(); if (mainViewModel != null && mainViewModel.originalWriteableBitmap != null && SelectedForExport) { try { int newLogoInsertHeight = ImageHeight - TopPadding - BottomPadding; var config = new Configuration(); var backgroundPixel = new Rgba32(); if (mainViewModel.UseTransparentBackground) { backgroundPixel = SixLabors.ImageSharp.Color.Transparent; } else { backgroundPixel.R = mainViewModel.BackgroundColour.R; backgroundPixel.G = mainViewModel.BackgroundColour.G; backgroundPixel.B = mainViewModel.BackgroundColour.B; backgroundPixel.A = mainViewModel.BackgroundColour.A; } newLogo = new Image <Rgba32>(config, ImageWidth, ImageHeight, backgroundPixel); var options = new ResizeOptions() { Mode = ResizeMode.Max, Position = AnchorPositionMode.Center, Size = new SixLabors.ImageSharp.Size(ImageWidth - TwentyPercentOf(ImageWidth), newLogoInsertHeight), Sampler = mainViewModel.SelectedResampler.Value }; var inStream = await mainViewModel.OriginalLogoFile.OpenReadAsync(); var resizedOriginal = Image <Rgba32> .Load(inStream.AsStreamForRead()); //resize the image to fit the GIF frame bounds resizedOriginal.Mutate(r => r.Resize(options)); //Get the top left point within the logo bounds var left = HalfOf(ImageWidth) - HalfOf(resizedOriginal.Width); var top = HalfOf(ImageHeight) - HalfOf(resizedOriginal.Height); newLogo.Mutate(w => w.DrawImage( resizedOriginal, new SixLabors.ImageSharp.Point(left, top), mainViewModel.SelectedBlendingMode.Value, mainViewModel.SelectedAlphaMode.Value, 1)); InMemoryRandomAccessStream myStream = new InMemoryRandomAccessStream(); SixLabors.ImageSharp.Formats.Png.PngEncoder encoder = new SixLabors.ImageSharp.Formats.Png.PngEncoder(); encoder.CompressionLevel = App.mainViewModel.SelectedPNGCompression.Value; newLogo.SaveAsPng(myStream.AsStream(), encoder); myStream.Seek(0); Logo = await BitmapFactory.FromStream(myStream); NotifyPropertyChanged("Logo"); result = true; } catch (Exception ex) { } SavedSuccessfully = false; } return(result); }
private async void msgBox_Paste(object sender, TextControlPasteEventArgs e) { // Mark the event as handled first. Otherwise, the // default paste action will happen, then the custom paste // action, and the user will see the text box content change. e.Handled = true; // Get content from the clipboard. var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent(); if (dataPackageView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text)) { var text = await dataPackageView.GetTextAsync(); if (text.Contains('\n')) { var messageDialog = new PasteTextDialog(text); // Add commands to the message dialog. messageDialog.PrimaryButtonClick += async(ev, i) => { var url = GetUploadUrl(Config.PasteText); StatusText.Text = "Pasting text..."; StatusArea.Visibility = Visibility.Visible; byte[] byteArray = Encoding.UTF8.GetBytes(messageDialog.PasteText); //byte[] byteArray = Encoding.ASCII.GetBytes(contents); MemoryStream stream = new MemoryStream(byteArray); await this.UploadFile(url, stream.AsInputStream()); }; await messageDialog.ShowAsync(); } else { msgBox.Text += text; msgBox.SelectionStart += text.Length; } } if (dataPackageView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Bitmap)) { StatusText.Text = "Uploading image..."; StatusArea.Visibility = Visibility.Visible; var image = await dataPackageView.GetBitmapAsync(); using (var stream = await image.OpenReadAsync()) { // Set the image source to the selected bitmap BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(stream); var messageDialog = new PasteImageDialog(bitmapImage); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); var pixels = await decoder.GetPixelDataAsync(); var outStream = new InMemoryRandomAccessStream(); // Add commands to the message dialog. messageDialog.PrimaryButtonClick += async(ev, i) => { // Create encoder for PNG var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream); // Get pixel data from decoder and set them for encoder encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore, // Alpha is not used decoder.OrientedPixelWidth, decoder.OrientedPixelHeight, decoder.DpiX, decoder.DpiY, pixels.DetachPixelData()); await encoder.FlushAsync(); // Write data to the stream var url = GetUploadUrl(Config.PasteImage); await this.UploadFile(url, outStream, "upload.png"); }; await messageDialog.ShowAsync(); } } }
private async Task RunProcess() { if (string.IsNullOrWhiteSpace(CommandLine.Text)) { return; } commandLineHistory.Add(CommandLine.Text); currentCommandLine = commandLineHistory.Count; bool isCmdAuthorized = true; Run cmdLineRun = new Run(); cmdLineRun.Foreground = new SolidColorBrush(Windows.UI.Colors.LightGray); cmdLineRun.FontWeight = FontWeights.Bold; cmdLineRun.Text = currentDirectory + "> " + CommandLine.Text + "\n"; Run stdOutRun = new Run(); Run stdErrRun = new Run(); stdErrRun.Foreground = new SolidColorBrush(Windows.UI.Colors.Red); var commandLineText = CommandLine.Text.Trim(); if (commandLineText.Equals("cls", StringComparison.CurrentCultureIgnoreCase)) { StdOutputText.Blocks.Clear(); return; } else if (commandLineText.StartsWith("cd ", StringComparison.CurrentCultureIgnoreCase) || commandLineText.Equals("cd", StringComparison.CurrentCultureIgnoreCase)) { stdErrRun.Text = resourceLoader.GetString("CdNotSupported"); } else if (commandLineText.Equals("exit", StringComparison.CurrentCultureIgnoreCase)) { NavigationUtils.GoBack(); } else { var standardOutput = new InMemoryRandomAccessStream(); var standardError = new InMemoryRandomAccessStream(); var options = new ProcessLauncherOptions { StandardOutput = standardOutput, StandardError = standardError }; try { var args = "/C \"cd \"" + currentDirectory + "\" & " + commandLineText + "\""; var result = await ProcessLauncher.RunToCompletionAsync(CommandLineProcesserExe, args, options); // First write std out using (var outStreamRedirect = standardOutput.GetInputStreamAt(0)) { using (var dataReader = new DataReader(outStreamRedirect)) { await ReadText(dataReader, stdOutRun); } } // Then write std err using (var errStreamRedirect = standardError.GetInputStreamAt(0)) { using (var dataReader = new DataReader(errStreamRedirect)) { await ReadText(dataReader, stdErrRun); } } } catch (UnauthorizedAccessException uex) { isCmdAuthorized = false; var errorMessage = uex.Message + "\n\n" + resourceLoader.GetString("CmdNotEnabled"); stdErrRun.Text = errorMessage; } catch (Exception ex) { var errorMessage = ex.Message + "\n" + ex.StackTrace + "\n"; stdErrRun.Text = errorMessage; } } await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { Paragraph paragraph = new Paragraph(); paragraph.Inlines.Add(cmdLineRun); paragraph.Inlines.Add(stdOutRun); paragraph.Inlines.Add(stdErrRun); if (!isCmdAuthorized) { InlineUIContainer uiContainer = new InlineUIContainer(); Button cmdEnableButton = new Button(); cmdEnableButton.Content = resourceLoader.GetString("EnableCmdText"); cmdEnableButton.Click += AccessButtonClicked; uiContainer.Child = cmdEnableButton; paragraph.Inlines.Add(uiContainer); } StdOutputText.Blocks.Add(paragraph); }); }
/// <summary> /// Fired when an image request is done. /// </summary> /// <param name="sender"></param> /// <param name="response"></param> public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove event listener ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; UpdateTypes type = (UpdateTypes)response.Request.Context; // Make sure we were successfully. if (response.Success) { try { // Get the target size Size targetImageSize = LastKnownScreenResoultion; // If we are desktop on mobile we want to do a bit larger than the screen res because // there is a sliding image animation when you switch to all apps. Lets make the width 30% larger. targetImageSize.Width *= 1.3; // Resize the image to fit nicely InMemoryRandomAccessStream image = await ResizeImage(response.ImageStream, targetImageSize); // Write the file await WriteImageToFile(image, type); } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Failed to write background image", e); } } // Indicate that this image is done. bool isDone = false; lock (m_getImageLock) { if (type == UpdateTypes.LockScreen) { m_lockScreenDoneCount++; isDone = m_lockScreenDoneCount >= m_lockScreenRequestCount; } else if (type == UpdateTypes.Band) { m_bandDoneCount++; isDone = m_bandDoneCount >= m_bandRequestCount; } else { m_desktopDoneCount++; isDone = m_desktopDoneCount >= m_desktopRequestCount; } } // if we are done done then tell the images to rotate and clean up. if (isDone) { // Set this high so we roll over. if (type == UpdateTypes.LockScreen) { CurrentLockScreenRotationIndex = 99; } else if (type == UpdateTypes.Band) { CurrentBandRotationIndex = 99; } else { CurrentDesktopRotationIndex = 99; } // Do the rotate await DoImageRotation(type); // If this is a lock screen update this might also be a desktop update. This happens when the lock screen and desktop // share the same subreddit, they share the same images. if (type == UpdateTypes.LockScreen && IsDeskopEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // Off set the two so they don't show the same image CurrentDesktopRotationIndex = 1; // We also need to update the desktop. await DoImageRotation(UpdateTypes.Desktop); } // We are done, set the last update time LastFullUpdate = DateTime.Now; // And kill the deferral ReleaseDeferral(type); // And set us to stopped. UnSetIsRunningIfDone(); } }
private async Task AnalyzeFrame() { textScanResults.Text = ""; var _bitmap = new RenderTargetBitmap(); await _bitmap.RenderAsync(previewImage); var pixels = await _bitmap.GetPixelsAsync(); int pixelW = _bitmap.PixelWidth; int pixelH = _bitmap.PixelHeight; byte[] usePixels = null; BitmapBounds cropBounds = new BitmapBounds() { X = 0, Y = 0, Width = 0, Height = 0 }; if (pixelW == 462 && pixelH == 864) { cropBounds = new BitmapBounds() { X = 1, Y = 46, Width = 458, Height = 813 }; } else if (pixelW == 440 && pixelH == 822) { cropBounds = new BitmapBounds() { X = 2, Y = 42, Width = 436, Height = 778 }; } if (cropBounds.Width > 0) { var scaledW = 338; var scaledH = 600; try { // transform: crop InMemoryRandomAccessStream streamCrop = new InMemoryRandomAccessStream(); { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, streamCrop); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)pixelW, (uint)pixelH, 96, 96, pixels.ToArray()); encoder.BitmapTransform.Bounds = cropBounds; await encoder.FlushAsync(); } InMemoryRandomAccessStream streamSize = new InMemoryRandomAccessStream(); { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(streamCrop); var pixelsProvider = await decoder.GetPixelDataAsync(); var inPixels = pixelsProvider.DetachPixelData(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, streamSize); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)cropBounds.Width, (uint)cropBounds.Height, 96, 96, inPixels); encoder.BitmapTransform.ScaledHeight = (uint)scaledH; encoder.BitmapTransform.ScaledWidth = (uint)scaledW; encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear; await encoder.FlushAsync(); } { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(streamSize); var pixelsProvider = await decoder.GetPixelDataAsync(); pixelW = (int)scaledW; pixelH = (int)scaledH; usePixels = pixelsProvider.DetachPixelData(); } streamCrop.Dispose(); streamSize.Dispose(); } catch (Exception ex) { } } try { byte[] pixelData = (usePixels != null) ? usePixels : pixels.ToArray(); var analyzeBitmap = ScreenshotUtilities.ConvertToFastBitmap(pixelData, pixelW, pixelH); foreach (var scanner in scanners) { object resultOb = scanner.Process(analyzeBitmap); if (resultOb != null) { textScanResults.Text = scanner.ScannerName + " found!\n\n" + resultOb; break; } } StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile exportFile = await storageFolder.CreateFileAsync("image-test.jpg", CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await exportFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); byte[] bytes = pixels.ToArray(); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)pixelW, (uint)pixelH, 200, 200, pixelData); await encoder.FlushAsync(); } } catch (Exception ex) { Console.WriteLine(ex); } }
/// <summary> /// Get cropped image /// </summary> /// <param name="originalImgFile"> Original image </param> /// <param name="startPoint"> Crop start point at original image coords </param> /// <param name="cropSize"> Crop size at original image coords </param> /// <param name="scale"> Scaling for final crop image respect to original size </param> /// <returns></returns> public static async Task <byte[]> GetCroppedImageAsync(StorageFile originalImgFile, Point startPoint, Size cropSize, double scale) { if (double.IsNaN(scale) || double.IsInfinity(scale)) { scale = 1.0; } // 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(cropSize.Height * scale); uint width = (uint)Math.Floor(cropSize.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 orientedScaledWidth = (uint)Math.Floor(decoder.OrientedPixelWidth * scale); uint orientedScaledHeight = (uint)Math.Floor(decoder.OrientedPixelHeight * scale); 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 > orientedScaledWidth) { startPointX = orientedScaledWidth - width; } if (startPointY + height > orientedScaledHeight) { startPointY = orientedScaledHeight - 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.InterpolationMode = BitmapInterpolationMode.Fant; transform.ScaledWidth = scaledWidth; transform.ScaledHeight = scaledHeight; // Get the cropped pixels within the bounds of transform. PixelDataProvider pix = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); using (var destinationStream = new InMemoryRandomAccessStream()) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream); encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, width, height, decoder.DpiX, decoder.DpiY, pix.DetachPixelData()); await encoder.FlushAsync(); var reader = new DataReader(destinationStream.GetInputStreamAt(0)); var bytes = new byte[destinationStream.Size]; await reader.LoadAsync((uint)destinationStream.Size); reader.ReadBytes(bytes); return(bytes); } } }
public static async Task <IRandomAccessStream> ResizeImage(this IRandomAccessStream imageStream, int width, int height, InterpolationMode interpolationMode, bool useDipUnits, bool allowUpscale, ImageInformation imageInformation = null) { if (useDipUnits) { width = width.PointsToPixels(); height = height.PointsToPixels(); } IRandomAccessStream resizedStream = imageStream; var decoder = await BitmapDecoder.CreateAsync(imageStream); if ((height > 0 && decoder.PixelHeight > height) || (width > 0 && decoder.PixelWidth > width) || allowUpscale) { using (imageStream) { resizedStream = new InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder); double widthRatio = (double)width / decoder.PixelWidth; double heightRatio = (double)height / decoder.PixelHeight; double scaleRatio = Math.Min(widthRatio, heightRatio); if (width == 0) { scaleRatio = heightRatio; } if (height == 0) { scaleRatio = widthRatio; } uint aspectHeight = (uint)Math.Floor(decoder.PixelHeight * scaleRatio); uint aspectWidth = (uint)Math.Floor(decoder.PixelWidth * scaleRatio); if (interpolationMode == InterpolationMode.None) { encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.NearestNeighbor; } else if (interpolationMode == InterpolationMode.Low) { encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear; } else if (interpolationMode == InterpolationMode.Medium) { encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Cubic; } else if (interpolationMode == InterpolationMode.High) { encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant; } else { encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear; } encoder.BitmapTransform.ScaledHeight = aspectHeight; encoder.BitmapTransform.ScaledWidth = aspectWidth; if (imageInformation != null) { imageInformation.SetOriginalSize((int)decoder.PixelWidth, (int)decoder.PixelHeight); imageInformation.SetCurrentSize((int)aspectWidth, (int)aspectHeight); } await encoder.FlushAsync(); resizedStream.Seek(0); } } return(resizedStream); }
private async void Flip_SelectionChanged(object sender, SelectionChangedEventArgs e) { LoadQueue.Enqueue(Flip.SelectedIndex); if (Interlocked.Exchange(ref LockResource, 1) == 0) { try { while (LoadQueue.TryDequeue(out int CurrentIndex) && !Cancellation.IsCancellationRequested) { //如果LastPageIndex < CurrentIndex,说明是向右翻页 if (LastPageIndex < CurrentIndex) { uint CurrentLoading = (uint)(CurrentIndex + 9); /* * MaxLoad始终取CurrentLoading达到过的最大值 * 同时检查要加载的页码是否小于等于最大值 * 可避免因向左翻页再向右翻页从而通过LastPageIndex < CurrentIndex检查 * 导致已加载过的页面重复加载的问题 */ if (CurrentLoading <= MaxLoad) { continue; } MaxLoad = CurrentLoading; if (CurrentLoading >= Pdf.PageCount) { Flip.SelectionChanged -= Flip_SelectionChanged; return; } foreach (uint Index in Enumerable.Range(PdfCollection.Count, (int)CurrentLoading - PdfCollection.Count + 1)) { if (Cancellation.IsCancellationRequested) { break; } using (PdfPage Page = Pdf.GetPage(Index)) using (InMemoryRandomAccessStream PageStream = new InMemoryRandomAccessStream()) { await Page.RenderToStreamAsync(PageStream, new PdfPageRenderOptions { DestinationHeight = Convert.ToUInt32(Page.Size.Height * 1.5), DestinationWidth = Convert.ToUInt32(Page.Size.Width * 1.5) }); BitmapImage DisplayImage = new BitmapImage(); PdfCollection.Add(DisplayImage); await DisplayImage.SetSourceAsync(PageStream); } } } LastPageIndex = CurrentIndex; } } finally { _ = Interlocked.Exchange(ref LockResource, 0); } } }
private void LaunchCmdProcess(string commandLineText) { var args = string.Format("/C \"{0}\"", commandLineText);; var standardOutput = new InMemoryRandomAccessStream(); var standardError = new InMemoryRandomAccessStream(); var options = new ProcessLauncherOptions { StandardOutput = standardOutput, StandardError = standardError, WorkingDirectory = GetWorkingDirectory() }; string stdErrRunText = string.Empty; CommandError commandError = CommandError.None; isProcessRunning = true; isProcessTimedOut = false; lastOutputTime = DateTime.Now; processLauncherOperation = ProcessLauncher.RunToCompletionAsync(CommandLineProcesserExe, args, options); processLauncherOperation.Completed = (operation, status) => { isProcessRunning = false; if (status == AsyncStatus.Canceled) { if (isProcessTimedOut) { commandError = CommandError.TimedOut; stdErrRunText = "\n" + string.Format(CommandTimeoutTextFormat, TimeOutAfterNoOutput.Seconds); } else { commandError = CommandError.Cancelled; stdErrRunText = "\n" + CommandCanceledText; } } else if (status == AsyncStatus.Error) { if (operation.ErrorCode.HResult == HRESULT_AccessDenied) { commandError = CommandError.NotAuthorized; stdErrRunText = string.Format(CmdNotEnabledTextFormat, EnableCommandLineProcesserRegCommand); } else if (operation.ErrorCode.HResult == HRESULT_InvalidDirectory) { commandError = CommandError.InvalidDirectory; stdErrRunText = string.Format(WorkingDirectoryInvalidTextFormat, options.WorkingDirectory); } else { commandError = CommandError.GenericError; stdErrRunText = string.Format(CommandLineErrorTextFormat, operation.ErrorCode.Message); } } if (commandError != CommandError.None) { ShowError(stdErrRunText); if (commandError == CommandError.NotAuthorized) { ShowAuthorizationUI(); } } if (commandError == CommandError.InvalidDirectory) { EnableCommandLineTextBox(true, WorkingDirectory); } else { EnableCommandLineTextBox(true, CommandLine); } }; var stdOutTask = ThreadPool.RunAsync(async(t) => { using (var outStreamRedirect = standardOutput.GetInputStreamAt(0)) { using (var streamReader = new StreamReader(outStreamRedirect.AsStreamForRead())) { await ReadText(streamReader); } } }).AsTask(); var stdErrTask = ThreadPool.RunAsync(async(t) => { using (var errStreamRedirect = standardError.GetInputStreamAt(0)) { using (var streamReader = new StreamReader(errStreamRedirect.AsStreamForRead())) { await ReadText(streamReader, isErrorRun: true); } } }).AsTask(); Task[] tasks = new Task[2] { stdOutTask, stdErrTask }; Task.WaitAll(tasks); }
private void StartResultReceivingThread() { IAsyncAction asyncAction = Windows.System.Threading.ThreadPool.RunAsync( async(workItem) => { while (true) { // receive current time at server string recvMsg = await receiveMsgAsync(_resultReceivingSocketReader); Debug.WriteLine(recvMsg); JsonObject obj = JsonValue.Parse(recvMsg).GetObject(); string status = null; string result = null; long frameID = -1; string engineID = null; try { status = obj.GetNamedString("status"); result = obj.GetNamedString("result"); frameID = (long)obj.GetNamedNumber("frame_id"); engineID = obj.GetNamedString("engine_id"); } catch (Exception) { Debug.WriteLine("the return message has no status field"); return; } ReceivedPacketInfo receivedPacketInfo = new ReceivedPacketInfo(frameID, engineID, status); receivedPacketInfo.setMsgRecvTime(GetTimeMillis()); if (!status.Equals("success")) { receivedPacketInfo.setGuidanceDoneTime(GetTimeMillis()); await _tokenController.ProcessReceivedPacket(receivedPacketInfo); continue; } if (result != null) { /* parsing result */ JsonObject resultJSON = JsonValue.Parse(result).GetObject(); // image guidance string imageFeedbackString = null; try { imageFeedbackString = resultJSON.GetNamedString("image"); } catch (Exception) { Debug.WriteLine("no image guidance found"); } if (imageFeedbackString != null) { IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(imageFeedbackString); //byte[] imageFeedbackBytes; //CryptographicBuffer.CopyToByteArray(buffer, out imageFeedbackBytes); using (var stream = new InMemoryRandomAccessStream()) { using (var dataWriter = new DataWriter(stream)) { dataWriter.WriteBuffer(buffer); await dataWriter.StoreAsync(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); SoftwareBitmap imageFeedback = await decoder.GetSoftwareBitmapAsync(); SoftwareBitmap imageFeedbackDisplay = SoftwareBitmap.Convert(imageFeedback, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { var sbSource = new SoftwareBitmapSource(); await sbSource.SetBitmapAsync(imageFeedbackDisplay); GuidanceImage.Source = sbSource; }); } } } // speech guidance string speechFeedback = null; try { speechFeedback = resultJSON.GetNamedString("speech"); } catch (Exception) { Debug.WriteLine("no speech guidance found"); } if (speechFeedback != null) { SpeechSynthesisStream stream = null; using (SpeechSynthesizer synthesizer = new SpeechSynthesizer()) { stream = await synthesizer.SynthesizeTextToStreamAsync(speechFeedback); } // Send the stream to the media object. await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { _mediaElement.SetSource(stream, stream.ContentType); _mediaElement.Play(); }); } receivedPacketInfo.setGuidanceDoneTime(GetTimeMillis()); await _tokenController.ProcessReceivedPacket(receivedPacketInfo); } } }); // A reference to the work item is cached so that we can trigger a // cancellation when the user presses the Cancel button. _resultReceivingWorkItem = asyncAction; }
private async void Authenticate_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrWhiteSpace(txtBoxPersonGroupId.Text)) { LogMessage($"Error: Person Group Id is empty."); } else { StorageFolder projectFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("FaceAuth", CreationCollisionOption.OpenIfExists); StorageFile file = await projectFolder.CreateFileAsync($"authenticate.jpg", CreationCollisionOption.ReplaceExisting); playSound.Play(); using (var captureStream = new InMemoryRandomAccessStream()) { await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream); using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { var decoder = await BitmapDecoder.CreateAsync(captureStream); var encoder = await BitmapEncoder.CreateForTranscodingAsync(fileStream, decoder); var properties = new BitmapPropertySet { { "System.Photo.Orientation", new BitmapTypedValue(PhotoOrientation.Normal, PropertyType.UInt16) } }; await encoder.BitmapProperties.SetPropertiesAsync(properties); await encoder.FlushAsync(); } } string testImageFile = ApplicationData.Current.LocalFolder.Path + "\\FaceAuth\\authenticate.jpg"; using (Stream s = File.OpenRead(testImageFile)) { var faces = await faceServiceClient.DetectAsync(s); if (faces.Length > 0) { var faceIds = faces.Select(face => face.FaceId).ToArray(); try { var results = await faceServiceClient.IdentifyAsync(txtBoxPersonGroupId.Text, faceIds); foreach (var identifyResult in results) { Debug.WriteLine("Result of face: {0}", identifyResult.FaceId); if (identifyResult.Candidates.Length == 0) { LogMessage("No one identified"); } else { // Get top 1 among all candidates returned var candidateId = identifyResult.Candidates[0].PersonId; var person = await faceServiceClient.GetPersonAsync(txtBoxPersonGroupId.Text, candidateId); LogMessage($"Identified as: {person.Name}"); } } } catch (Exception error) { LogMessage($"{error.Message}"); return; } } else { LogMessage("No face identified. Please try again."); } } } }
public static async Task <TLPhotoSizeBase> GetFileThumbAsync(StorageFile file) { try { const int imageSize = 60; IRandomAccessStream thumb = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, imageSize, ThumbnailOptions.ResizeThumbnail); if (((StorageItemThumbnail)thumb).ContentType == "image/png") { var tempThumb = new InMemoryRandomAccessStream(); var decoder = await BitmapDecoder.CreateAsync(thumb); var pixels = await decoder.GetPixelDataAsync(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, tempThumb); encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiY, pixels.DetachPixelData()); await encoder.FlushAsync(); thumb = tempThumb; } var volumeId = TLLong.Random(); var localId = TLInt.Random(); var secret = TLLong.Random(); var thumbLocation = new TLFileLocation { DCId = new TLInt(0), VolumeId = volumeId, LocalId = localId, Secret = secret, }; var fileName = String.Format("{0}_{1}_{2}.jpg", thumbLocation.VolumeId, thumbLocation.LocalId, thumbLocation.Secret); var thumbFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); var thumbBuffer = new Windows.Storage.Streams.Buffer(Convert.ToUInt32(thumb.Size)); var iBuf = await thumb.ReadAsync(thumbBuffer, thumbBuffer.Capacity, InputStreamOptions.None); using (var thumbStream = await thumbFile.OpenAsync(FileAccessMode.ReadWrite)) { await thumbStream.WriteAsync(iBuf); } var thumbSize = new TLPhotoSize { W = new TLInt(imageSize), H = new TLInt(imageSize), Size = new TLInt((int)thumb.Size), Type = TLString.Empty, Location = thumbLocation, Bytes = TLString.FromBigEndianData(thumbBuffer.ToArray()) }; return(thumbSize); } catch (Exception ex) { Telegram.Api.Helpers.Execute.ShowDebugMessage("GetFileThumbAsync exception " + ex); } return(null); }
async void CopyButton_Click(object sender, RoutedEventArgs e) { // Generate a bitmap for our clipboard content. RenderTargetBitmap target = new RenderTargetBitmap(); await target.RenderAsync(ClipboardContent); IBuffer buffer = await target.GetPixelsAsync(); SoftwareBitmap bitmap = SoftwareBitmap.CreateCopyFromBuffer(buffer, BitmapPixelFormat.Bgra8, target.PixelWidth, target.PixelHeight, BitmapAlphaMode.Premultiplied); // Save the bitmap to a stream. InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); encoder.SetSoftwareBitmap(bitmap); await encoder.FlushAsync(); stream.Seek(0); // Create a DataPackage to hold the bitmap and text. var dataPackage = new DataPackage(); dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream)); dataPackage.SetText(ClipboardContent.Text); ClipboardContentOptions options = new ClipboardContentOptions(); // Customize clipboard history. if (IncludeAllInHistory.IsChecked.Value) { // This is the default, but set it explicitly for expository purposes. options.IsAllowedInHistory = true; } else if (IncludeTextInHistory.IsChecked.Value) { // This is the default, but set it explicitly for expository purposes. options.IsAllowedInHistory = true; // Allow only text to go into the history. options.HistoryFormats.Add(StandardDataFormats.Text); } else { // Exclude from history. options.IsAllowedInHistory = false; } // Customize clipboard roaming. if (AllowAllToRoam.IsChecked.Value) { // This is the default, but set it explicitly for expository purposes. options.IsRoamable = true; } else if (AllowTextToRoam.IsChecked.Value) { // This is the default, but set it explicitly for expository purposes. options.IsRoamable = true; // Allow only text to roam. options.RoamingFormats.Add(StandardDataFormats.Text); } else { // Exclude from roaming. options.IsRoamable = false; } // Set the data package to the clipboard with our custom options. if (Clipboard.SetContentWithOptions(dataPackage, options)) { rootPage.NotifyUser("Text and bitmap have been copied to clipboard.", NotifyType.StatusMessage); } else { // Copying data to Clipboard can potentially fail - for example, if another application is holding Clipboard open rootPage.NotifyUser("Error copying content to clipboard.", NotifyType.ErrorMessage); } }
private async void _cvMain_SendMessage(object sender, ContentViewEventArgs e) { var cv = (ContentViewEventArgs)e; if (cv.Type == "LoadCompleted") { if (cv.Source == null || cv.Source.OriginalString.Length == 0) { return; } _LoadCompletedSource?.Raise(this, new LoadCompletedEventArgs() { ActualHeight = _cvMain.ActualHeight, ActualWidth = _cvMain.ActualWidth, SearchQuery = cv.Source.OriginalString }); var uriHash = FlickrNet.UtilityMethods.MD5Hash(cv.Source.OriginalString); // e.Uri.OriginalString); await Task.Delay(1000); //capture screenshot using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream()) { await _cvMain.Renderer?.CaptureThumbnail(ms); //img: Banner 400 width //ms.Seek(0); await X.Services.Image.Service.Instance.GenerateResizedImageAsync(400, _cvMain.ActualWidth, _cvMain.ActualHeight, ms, uriHash + ".png", X.Services.Image.Service.location.MediumFolder); //img: Thumbnail ms.Seek(0); await X.Services.Image.Service.Instance.GenerateResizedImageAsync(180, _cvMain.ActualWidth, _cvMain.ActualHeight, ms, uriHash + ".png", X.Services.Image.Service.location.ThumbFolder); //img: Tile ms.Seek(0); await X.Services.Image.Service.Instance.GenerateResizedImageAsync(71, _cvMain.ActualWidth, _cvMain.ActualHeight, ms, uriHash + ".png", X.Services.Image.Service.location.TileFolder, 71); ms.Seek(0); await X.Services.Image.Service.Instance.GenerateResizedImageAsync(150, _cvMain.ActualWidth, _cvMain.ActualHeight, ms, uriHash + "-150x150.png", X.Services.Image.Service.location.TileFolder, 150); ms.Seek(0); await X.Services.Image.Service.Instance.GenerateResizedImageAsync(310, _cvMain.ActualWidth, _cvMain.ActualHeight, ms, uriHash + "-310x150.png", X.Services.Image.Service.location.TileFolder, 150); ms.Seek(0); await X.Services.Image.Service.Instance.GenerateResizedImageAsync(310, _cvMain.ActualWidth, _cvMain.ActualHeight, ms, uriHash + "-310x310.png", X.Services.Image.Service.location.TileFolder, 310); //update tile X.Services.Tile.Service.UpdatePrimaryTile("X.Browser", "ms-appdata:///local/tile/" + uriHash + "-150x150.png", "ms-appdata:///local/tile/" + uriHash + "-310x150.png", "ms-appdata:///local/tile/" + uriHash + "-310x310.png", "ms-appdata:///local/tile/" + uriHash + ".png" ); } } else if (cv.Type == "NavigationFailed") { _prLoading.IsActive = false; _prLoading.Visibility = Visibility.Collapsed; } else if (cv.Type == "NavigationCompleted") { _prLoading.IsActive = false; _prLoading.Visibility = Visibility.Collapsed; } else if (cv.Type == "NavigationStarting") { _prLoading.IsActive = true; _prLoading.Visibility = Visibility.Visible; } }
/* * 分为地图查找和交通路况显示,由checkbox控制 */ private async void searchMap(object sender, RoutedEventArgs e) { currentCity = ""; if (searchTraffic.IsChecked == false) { if (mapSearchBlock.Text != "") { string url = "http://restapi.amap.com/v3/geocode/geo?key=5fd3b8bd943a505ccfec387943bba945&address=" + mapSearchBlock.Text; HttpClient client = new HttpClient(); string result = await client.GetStringAsync(url); JObject jo = (JObject)JsonConvert.DeserializeObject(result); if (jo["status"].ToString() == "0" || jo["count"].ToString() == "0") { var i = new MessageDialog("查无此地区").ShowAsync(); } else { JArray ja = (JArray)jo["geocodes"]; string location = ja[0]["location"].ToString(); currentCity = ja[0]["formatted_address"].ToString(); string url2 = "http://restapi.amap.com/v3/staticmap?key=5fd3b8bd943a505ccfec387943bba945&location=" + location + "&zoom=10&size=731*458&labels=" + mapSearchBlock.Text + ",2,0,16,0xFFFFFF,0x008000:" + location; HttpResponseMessage response = await client.GetAsync(url2); BitmapImage bitmap = new BitmapImage(); Stream stream = await response.Content.ReadAsStreamAsync(); IInputStream inputStream = stream.AsInputStream(); using (IRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream()) { using (IOutputStream outputStream = randomAccessStream.GetOutputStreamAt(0)) { await RandomAccessStream.CopyAsync(inputStream, outputStream); randomAccessStream.Seek(0); bitmap.SetSource(randomAccessStream); map.Source = bitmap; } } } } } else { if (mapSearchBlock.Text != "") { string url = "http://restapi.amap.com/v3/geocode/geo?key=5fd3b8bd943a505ccfec387943bba945&address=" + mapSearchBlock.Text; HttpClient client = new HttpClient(); string result = await client.GetStringAsync(url); JObject jo = (JObject)JsonConvert.DeserializeObject(result); if (jo["status"].ToString() == "0" || jo["count"].ToString() == "0") { var i = new MessageDialog("查无此地区").ShowAsync(); } else { JArray ja = (JArray)jo["geocodes"]; string location = ja[0]["location"].ToString(); currentCity = ja[0]["formatted_address"].ToString(); string url2 = "http://restapi.amap.com/v3/staticmap?key=5fd3b8bd943a505ccfec387943bba945&zoom=10&size=731*458&traffic=1&location=" + location; HttpResponseMessage response = await client.GetAsync(url2); BitmapImage bitmap = new BitmapImage(); Stream stream = await response.Content.ReadAsStreamAsync(); IInputStream inputStream = stream.AsInputStream(); using (IRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream()) { using (IOutputStream outputStream = randomAccessStream.GetOutputStreamAt(0)) { await RandomAccessStream.CopyAsync(inputStream, outputStream); randomAccessStream.Seek(0); bitmap.SetSource(randomAccessStream); map.Source = bitmap; } } } } } }