public Piece3(float PieceX, float PieceY, string PieceColor, CanvasBitmap PieceImage, bool PieceIsActive) : base(PieceX, PieceY, PieceColor, PieceImage, PieceIsActive) { }
public void ProcessFrame(ProcessVideoFrameContext context) { //using (CanvasBitmap inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface)) //using (CanvasRenderTarget renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface)) //using (CanvasDrawingSession ds = renderTarget.CreateDrawingSession()) //{ // var effect = new EdgeDetectionEffect // { // Source = inputBitmap, // Amount = this.Amount // }; // ds.DrawImage(effect); //} // When using SupportedMemoryTypes => MediaMemoryTypes.GpuAndCpu we need to check if we're using GPU or CPU for the frame // If we're on GPU, use InputFrame.Direct3DSurface if (context.InputFrame.SoftwareBitmap == null) { using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface)) using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface)) using (var ds = renderTarget.CreateDrawingSession()) { var effect = new EdgeDetectionEffect { Source = inputBitmap, Amount = this.Amount }; ds.DrawImage(effect); } return; } // If we're on CPU, use InputFrame.SoftwareBitmap if (context.InputFrame.Direct3DSurface == null) { #if DEBUG if (!debugOutputFlag) { Debug.WriteLine($"PixelFormat: {context.InputFrame.SoftwareBitmap.BitmapPixelFormat}"); debugOutputFlag = true; } #endif // ********** Test for using SoftwareBitmap.Convert ********* // //using (var inputBitmap = CanvasBitmap.CreateFromSoftwareBitmap( // _canvasDevice, // SoftwareBitmap.Convert(context.InputFrame.SoftwareBitmap, context.InputFrame.SoftwareBitmap.BitmapPixelFormat))) //PixelFormat: Bgra8 //using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface)) //using (var ds = renderTarget.CreateDrawingSession()) //{ // var effect = new EdgeDetectionEffect // { // Source = inputBitmap, // Amount = this.Amount // }; // ds.DrawImage(effect); //} // ********************************************************** // // InputFrame's raw pixels byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight]; context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer()); using (var inputBitmap = CanvasBitmap.CreateFromBytes( _canvasDevice, inputFrameBytes, context.InputFrame.SoftwareBitmap.PixelWidth, context.InputFrame.SoftwareBitmap.PixelHeight, context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat())) using (var renderTarget = new CanvasRenderTarget( _canvasDevice, context.OutputFrame.SoftwareBitmap.PixelWidth, context.OutputFrame.SoftwareBitmap.PixelHeight, (float)context.OutputFrame.SoftwareBitmap.DpiX, context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(), CanvasAlphaMode.Premultiplied)) { using (var ds = renderTarget.CreateDrawingSession()) { var effect = new EdgeDetectionEffect { Source = inputBitmap, Amount = this.Amount }; ds.DrawImage(effect); } } } }
/// <summary> /// Sprite betoltes /// </summary> /// <param name="spriteSheetDataPath">sheet_data helye</param> /// <param name="spriteSheetPath">Spritesheet helye</param> /// <returns></returns> public static async Task <bool> LoadSheet(string spriteSheetDataPath, string spriteSheetPath) { if (canvas == null) { return(false); } //Load whole sheet byte[] bytes; int sheetWidth; int sheetHeight; try { StorageFile sheet_file = await StorageFile.GetFileFromPathAsync(Environment.CurrentDirectory + spriteSheetPath); IRandomAccessStream stream = await RandomAccessStreamReference.CreateFromFile(sheet_file).OpenReadAsync(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); PixelDataProvider pixelData = await decoder.GetPixelDataAsync(); bytes = pixelData.DetachPixelData(); sheetWidth = Convert.ToInt32(decoder.PixelWidth); sheetHeight = Convert.ToInt32(decoder.PixelHeight); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } //Load sheet data string data; try { StorageFile data_file = await StorageFile.GetFileFromPathAsync(Environment.CurrentDirectory + spriteSheetDataPath); data = await FileIO.ReadTextAsync(data_file); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } int x, y, id, xAbs, yAbs, w, h; bool solid, penetrateable; string name; //Sheet data proccesing MatchCollection matches = Regex.Matches(data, @"(\w+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d) (\d)"); foreach (Match match in matches) { name = match.Groups[1].Value; x = int.Parse(match.Groups[2].Value); y = int.Parse(match.Groups[3].Value); w = int.Parse(match.Groups[4].Value); h = int.Parse(match.Groups[5].Value); id = int.Parse(match.Groups[6].Value); byte[] bitmap_bytes = new byte[w * h * 4]; //Out of bound check if (x * w > sheetWidth || y * h > sheetHeight || x < 0 || y < 0) { continue; } for (int by = 0; by < h; by++) { yAbs = by + y * h; for (int bx = 0; bx < w * 4; bx++) { xAbs = bx + x * w * 4; bitmap_bytes[bx + by * w * 4] = bytes[xAbs + yAbs * sheetWidth * 4]; } } CanvasBitmap bitmap = CanvasBitmap.CreateFromBytes(canvas, bitmap_bytes, w, h, DirectXPixelFormat.R8G8B8A8UIntNormalized); if (bitmap == null) { return(false); } Sprite sprite = new Sprite(w, h, bitmap); sprites.Add(id, sprite); table.Add(name, id); //Data for tiles solid = 1 == int.Parse(match.Groups[7].Value); penetrateable = 1 == int.Parse(match.Groups[8].Value); //Create tiles based on sprites new Tile(solid, penetrateable, id); if (sprites.ContainsKey(id)) { continue; } else { return(false); } } return(true); }
public async Task UnpackToMp4() { MediaComposition mediacomposition = new MediaComposition(); StorageFolder pictureFolder = KnownFolders.SavedPictures; { int len = simpleRecorder.unpackList.Count; for (int i = 0; i < len; i++) { UnpackItem unpackItem = simpleRecorder.unpackList[i]; 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 = await CanvasBitmap.LoadAsync(CanvasDevice.GetSharedDevice(), inMemoryRandomAccessStream); if (tempBitmap != null) { CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), tempBitmap.SizeInPixels.Width, tempBitmap.SizeInPixels.Height, 96); using (CanvasDrawingSession session = canvasRenderTarget.CreateDrawingSession()) { session.Clear(Colors.Black); session.DrawImage(tempBitmap); TimeSpan frameTime30Mil = TimeSpan.FromMilliseconds(30f); TimeSpan frameTime = unpackItem.frameTime; //if (frameTime < frameTime30Mil) // frameTime = frameTime30Mil; MediaClip mediaclip = MediaClip.CreateFromSurface(canvasRenderTarget, frameTime); mediacomposition.Clips.Add(mediaclip); } string str = "Adding Clips " + (i + 1).ToString() + " / " + len.ToString(); if (i == len - 1) { str += " ... Please wait for file rendering ..."; } TextOutput.Text = str; } } //free up memory recources as each frame is unpacked if (unpackItem.compressedBuffer != null) { unpackItem.compressedBuffer = null; } } //for } StorageFile mp4file = null; if (simpleRecorder.savefile != null) { mp4file = simpleRecorder.savefile; } else { string mp4filename = "SavedVideo" + ".mp4"; mp4file = await pictureFolder.CreateFileAsync( mp4filename, CreationCollisionOption.ReplaceExisting); } //await mediacomposition.RenderToFileAsync(mp4file, MediaTrimmingPreference.Precise, MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p)); var rendering = mediacomposition.RenderToFileAsync(mp4file, MediaTrimmingPreference.Precise, MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p)); rendering.Progress += ProgressReport; rendering.Completed += CompletedReport; }
public static async Task <bool> LoadSheet(string spriteSheetDataPath, string spriteSheetPath) { if (_canvas == null) { return(false); } //Load whole sheet byte[] bytes; int sheetWidth; int sheetHeight; try { StorageFile sheet_file = await StorageFile.GetFileFromPathAsync(Environment.CurrentDirectory + spriteSheetPath); IRandomAccessStream stream = await RandomAccessStreamReference.CreateFromFile(sheet_file).OpenReadAsync(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); PixelDataProvider pixelData = await decoder.GetPixelDataAsync(); bytes = pixelData.DetachPixelData(); sheetWidth = Convert.ToInt32(decoder.PixelWidth); sheetHeight = Convert.ToInt32(decoder.PixelHeight); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } //Load sheet data string data; try { StorageFile data_file = await StorageFile.GetFileFromPathAsync(Environment.CurrentDirectory + spriteSheetDataPath); data = await FileIO.ReadTextAsync(data_file); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } //Process sheet data string name; int rate, length; int x, y, xAbs, yAbs; byte[] bitmap_bytes; int WDS; int HDS; //Sheet data proccesing MatchCollection matches = Regex.Matches(data, @"(\w+) (\d+) (\d+) (\d+) (\d+) (\d+) (\d+)"); foreach (Match match in matches) { //AnimatedSprite data name = match.Groups[1].Value; length = int.Parse(match.Groups[2].Value); rate = int.Parse(match.Groups[3].Value); //Dissection start point x = int.Parse(match.Groups[4].Value); y = int.Parse(match.Groups[5].Value); WDS = int.Parse(match.Groups[6].Value); HDS = int.Parse(match.Groups[7].Value); bitmap_bytes = new byte[WDS * HDS * 4]; CanvasBitmap[] bitmaps = new CanvasBitmap[length]; int ySave = y; for (int i = 0; i < length; i++) { y = ySave + i * HDS; //Out of bound check if ((x + WDS) > sheetWidth || (y + HDS) > sheetHeight || x < 0 || y < 0) { continue; } for (int by = 0; by < HDS; by++) { yAbs = by + y; for (int bx = 0; bx < WDS * 4; bx++) { xAbs = bx + x * 4; bitmap_bytes[bx + by * WDS * 4] = bytes[xAbs + yAbs * sheetWidth * 4]; } } CanvasBitmap bitmap = CanvasBitmap.CreateFromBytes(_canvas, bitmap_bytes, WDS, HDS, DirectXPixelFormat.R8G8B8A8UIntNormalized); if (bitmap == null) { throw new Exception("Couldn't create bitmap: " + name); } bitmaps[i] = bitmap; } if (_animatedSprites.ContainsKey(name)) { return(false); } _animatedSprites.Add(name, new AnimatedSprite(name, length, rate, bitmaps)); } return(true); }
private async void OpenExecute() { var picker = new FileOpenPicker(); picker.ViewMode = PickerViewMode.Thumbnail; picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.FileTypeFilter.AddRange(Constants.PhotoTypes); var file = await picker.PickSingleFileAsync(); if (file == null) { return; } var stream = await file.OpenReadAsync(); { var points = Native.MrzRecognizer.FindCornerPoints(stream); if (points != null) { var pointsScale = 1.0f; Vector2 topLeft = new Vector2(points[0], points[1]), topRight = new Vector2(points[2], points[3]), bottomLeft = new Vector2(points[4], points[5]), bottomRight = new Vector2(points[6], points[7]); if (topRight.X < topLeft.X) { Vector2 tmp = topRight; topRight = topLeft; topLeft = tmp; tmp = bottomRight; bottomRight = bottomLeft; bottomLeft = tmp; } double topLength = Hypotenuse(topRight.X - topLeft.X, topRight.Y - topLeft.Y); double bottomLength = Hypotenuse(bottomRight.X - bottomLeft.X, bottomRight.Y - bottomLeft.Y); double leftLength = Hypotenuse(bottomLeft.X - topLeft.X, bottomLeft.Y - topLeft.Y); double rightLength = Hypotenuse(bottomRight.X - topRight.X, bottomRight.Y - topRight.Y); double tlRatio = topLength / leftLength; double trRatio = topLength / rightLength; double blRatio = bottomLength / leftLength; double brRatio = bottomLength / rightLength; if ((tlRatio >= 1.35 && tlRatio <= 1.75) && (blRatio >= 1.35 && blRatio <= 1.75) && (trRatio >= 1.35 && trRatio <= 1.75) && (brRatio >= 1.35 && brRatio <= 1.75)) { double avgRatio = (tlRatio + trRatio + blRatio + brRatio) / 4.0; float newWidth = 1024; float newHeight = (int)Math.Round(1024 / avgRatio); stream.Seek(0); var props = await file.Properties.GetImagePropertiesAsync(); var decoder = await BitmapDecoder.CreateAsync(stream); var pixelData2 = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage); var pixelData = pixelData2.DetachPixelData(); Source.Width = newWidth; Source.Height = newHeight; CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget offscreen = new CanvasRenderTarget(device, newWidth, newHeight, 96); using (CanvasDrawingSession ds = offscreen.CreateDrawingSession()) { ds.Clear(Colors.Red); var bitmap = CanvasBitmap.CreateFromBytes(device, pixelData, (int)decoder.PixelWidth, (int)decoder.PixelHeight, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized); var t = general2DProjection( topLeft.X, topLeft.Y, 0, 0, topRight.X, topRight.Y, newWidth, 0, bottomRight.X, bottomRight.Y, newWidth, newHeight, bottomLeft.X, bottomLeft.Y, 0, newHeight); for (int i = 0; i != 9; ++i) t[i] = t[i] / t[8]; var matrix = new Matrix4x4(t[0], t[3], 0, t[6], t[1], t[4], 0, t[7], 0, 0, 1, 0, t[2], t[5], 0, t[8]); ds.DrawImage(bitmap, 0, 0, new Rect(0, 0, props.Width, props.Height), 1, CanvasImageInterpolation.Linear, matrix); } using (var memory = new InMemoryRandomAccessStream()) { await offscreen.SaveAsync(memory, CanvasBitmapFileFormat.Png, 1); memory.Seek(0); var charRects = Native.MrzRecognizer.BinarizeAndFindCharacters(memory, out string mrz); offscreen = new CanvasRenderTarget(device, newWidth, newHeight, 96); using (CanvasDrawingSession ds = offscreen.CreateDrawingSession()) { ds.Clear(Colors.Red); var bitmap = CanvasBitmap.CreateFromBytes(device, pixelData, (int)decoder.PixelWidth, (int)decoder.PixelHeight, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized); var t = general2DProjection( topLeft.X, topLeft.Y, 0, 0, topRight.X, topRight.Y, newWidth, 0, bottomRight.X, bottomRight.Y, newWidth, newHeight, bottomLeft.X, bottomLeft.Y, 0, newHeight); for (int i = 0; i != 9; ++i) t[i] = t[i] / t[8]; var matrix = new Matrix4x4(t[0], t[3], 0, t[6], t[1], t[4], 0, t[7], 0, 0, 1, 0, t[2], t[5], 0, t[8]); ds.DrawImage(bitmap, 0, 0, new Rect(0, 0, props.Width, props.Height), 1, CanvasImageInterpolation.Linear, matrix); foreach (var line in charRects) { foreach (var box in line) { ds.DrawRectangle(box, Colors.Red, 2); } } } await offscreen.SaveAsync(memory, CanvasBitmapFileFormat.Png, 1); var bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(memory); Source.Source = bitmapImage; var result = ParseMrz(mrz); String type = "unknown"; if (result.Type == RecognitionResult.TYPE_PASSPORT) type = "Passport"; else if (result.Type == RecognitionResult.TYPE_ID) type = "ID"; String info = "Type: " + type + "\n" + "Number: " + result.Number + "\n" + "First name: " + result.FirstName + "\nLast name: " + result.LastName + "\n" + "Gender: " + (result.Gender == RecognitionResult.GENDER_MALE ? "male" : (result.Gender == RecognitionResult.GENDER_FEMALE ? "female" : "unknown")) + "\n" + "Birth date: " + result.birthDay + "." + result.birthMonth + "." + result.birthYear + "\n" + "Expiry date: " + (result.DoesNotExpire ? "does not expire" : (result.expiryDay + "." + result.expiryMonth + "." + result.expiryYear)) + "\n" + "Issuing country: " + (result.IssuingCountry) + "\n" + "Nationality: " + (result.Nationality) + "\n"; Mrz.Text = result.Raw; Info.Text = info; } } stream.Dispose(); //Lines.Children.Clear(); //Lines.Children.Add(new Line { X1 = topLeft.X, Y1 = topLeft.Y, X2 = topRight.X, Y2 = topRight.Y, Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = 2 }); //Lines.Children.Add(new Line { X1 = topRight.X, Y1 = topRight.Y, X2 = bottomRight.X, Y2 = bottomRight.Y, Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = 2 }); //Lines.Children.Add(new Line { X1 = bottomRight.X, Y1 = bottomRight.Y, X2 = bottomLeft.X, Y2 = bottomLeft.Y, Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = 2 }); //Lines.Children.Add(new Line { X1 = bottomLeft.X, Y1 = bottomLeft.Y, X2 = topLeft.X, Y2 = topLeft.Y, Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = 2 }); } } }
public static Task <string> CreateColorAtPointError(this CanvasBitmap bitmap, WColor expectedColor, int x, int y) => CreateColorError(bitmap, $"Expected {expectedColor} at point {x},{y} in renderered view.");
private async void LoadResources() { var sharedDevice = CanvasDevice.GetSharedDevice(); MenuButtonAnimationSource = await CanvasBitmap.LoadAsync(sharedDevice, MenuButtonAnimationSourceUri); }
/// <summary> /// Scale image file based on display information /// </summary> public static Transform2DEffect Img(CanvasBitmap source) { return(ScaleImage(source, MainPage.scaleWidth, MainPage.scaleHeight)); }
async Task CaptureThumbnailFromCustomSource(ICustomThumbnailSource customSource) { var bitmap = await CanvasBitmap.LoadAsync(new CanvasDevice(), customSource.Thumbnail); await SaveThumbnails(bitmap); }
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(); parent.msg(debugstr); } threadcounter++; if (threadcounter > 200000) { threadcounter = 0; } if (currentFrame != null) { ///need to handle device lost 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 = videostream.Position; await RandomAccessStream.CopyAsync(inputstream, videostream); await videostream.FlushAsync(); //works, but significant slow down DateTime currentTimeLocal = DateTime.Now; TimeSpan diff = currentTimeLocal - previousRecordTime; previousRecordTime = currentTimeLocal; /// await RandomAccessStream.CopyAsync(inputstream, memorystream); /// await memorystream.FlushAsync(); //works, but significant slow down UnpackItem unpackItem = new UnpackItem(); unpackItem.pos = _currentVideoStreamPos; unpackItem.length = currentFrameLength; unpackItem.frameTime = diff; 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(20); } } await CloseVideoStream(); int len = unpackList.Count; DateTime currentTime = DateTime.Now; TimeSpan elpasedTime = currentTime - initialRecordTime; string debugstrx = "Num frame: " + len.ToString() + " Threadcounter: " + threadcounter.ToString(); debugstrx += " StreamSize: " + ((int)(videostream.Size / 1024.0)).ToString() + "KB 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 parent.UnpackToMp4(); }
private async Task LoadResourcesAsync(CanvasControl canvas) { // Async load resources Head = await CanvasBitmap.LoadAsync(canvas, new Uri("ms-appx:///Assets/Snake/Head.png")); }
internal override void tileImage(CanvasBitmap canvasBitmap, int x, int y, int w, int h) { pendingRenderingOperations.Add(new TileImagePainter(clip, canvasBitmap, x, y, w, h, alpha)); }
internal override void drawImage(CanvasBitmap canvasBitmap, int x, int y) { pendingRenderingOperations.Add(new DrawImagePainter(clip, canvasBitmap, x, y, alpha)); }
public void Invalidate() { var animations = _animations; if (animations == null || _canvas == null || _device == null) { return; } var index = _index; var framesPerUpdate = _limitFps ? _animationFrameRate < 60 ? 1 : 2 : 1; var enqueue = false; if (_animationFrameRate < 60 && !_limitFps) { if (_skipFrame) { _skipFrame = false; return; } _skipFrame = true; } for (int i = 0; i < animations.Length; i++) { if (_bitmaps[i] == null) { if (animations[i] != null) { _bitmaps[i] = CanvasBitmap.CreateFromBytes(_device, _reusableBuffer, _frameSize.Width, _frameSize.Height, DirectXPixelFormat.B8G8R8A8UIntNormalized); } else { continue; } } animations[i].RenderSync(_bitmaps[i], index[i]); if (i == 1 && !_isLoopingEnabled[i]) { IndexChanged?.Invoke(this, index[i]); } if (_startIndex[i] <= index[1] && index[i] + framesPerUpdate < animations[i].TotalFrame) { _index[i] += framesPerUpdate; } else { if (_isLoopingEnabled[i]) { _index[i] = 0; if (i == 1 && _value == 0 && _enqueued != 0 && _enqueuedState != null) { enqueue = true; } } else if (i == 1) { _subscribed = false; _ = Dispatcher.RunIdleAsync(idle => Subscribe(false)); } } } if (enqueue) { _subscribed = false; _ = Dispatcher.RunIdleAsync(idle => SetValue(_enqueuedState, _enqueued)); } }
/// <summary> /// Scale image file uniformly based on display information /// </summary> /// <param name="sizeMultiplier">A multiplier to scale the image with</param> public static Transform2DEffect ImgUniform(CanvasBitmap source, float sizeMultiplier) { float scale = MathF.Min(MainPage.scaleWidth, MainPage.scaleHeight) * sizeMultiplier; return(ScaleImage(source, scale, scale)); }
async Task createResourcesAsync(CanvasAnimatedControl sender) { Sun = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/icons/Sun.png")); Moon = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/icons/Moon.png")); }
/// <summary> /// Stretch image to fill in the whole window /// </summary> public static Transform2DEffect Stretch(CanvasBitmap source) { return(ScaleImage(source, (float)(MainPage.bounds.X / source.Size.Width), (float)(MainPage.bounds.Y / source.Size.Height))); }
public MainPage() { this.InitializeComponent(); // 1. Win2D のビットマップ (ここに画像を読み込ませる) CanvasBitmap bitmap = null; // 2. Win2D Canvas の CreateResources イベントで画像を読み込む win2DCanvas.CreateResources += (s, e) => { // s is CanvasControl // e is CanvasCreateResourcesEventArgs e.TrackAsyncAction(LoadImageAsync(s).AsAsyncAction()); async Task LoadImageAsync(CanvasControl canvas) { bitmap = await CanvasBitmap.LoadAsync(canvas, "Assets/ClockPanel.png"); // ここでは 1 個だけだが、必要なだけ何個でも! } }; // 3. Win2D Canvas の Draw イベントで bitmap を表示する win2DCanvas.Draw += (s, e) => { // s is CanvasControl // e is CanvasDrawEventArgs // 単にビットマップをそのまま表示するなら、↓これだけ //e.DrawingSession.DrawImage(bitmap, 0, 0); // ビットマップにエフェクトを掛ける Color c = colorPicker.Color; float r = c.R / 255.0f; float g = c.G / 255.0f; float b = c.B / 255.0f; var colorMatrixEffect = new ColorMatrixEffect { Source = bitmap, ColorMatrix = new Matrix5x4 { M11 = r, M12 = 0, M13 = 0, M14 = 0, M21 = 0, M22 = g, M23 = 0, M24 = 0, M31 = 0, M32 = 0, M33 = b, M34 = 0, M41 = 0, M42 = 0, M43 = 0, M44 = 1, M51 = 0, M52 = 0, M53 = 0, M54 = 0 }, }; // エフェクトを掛けた結果を表示する e.DrawingSession.DrawImage(colorMatrixEffect, 0, 0); // ※ ここでは、Win2D Canvas を ViewBox の中に入れているため、表示サイズの調整が必要。 // (Win2D Canvas を固定スケールで表示しているなら、つまり、普通に使っているなら、不要) canvasGrid.Width = bitmap.Size.Width * 2; canvasGrid.Height = bitmap.Size.Height; }; // 4. 必要に応じて Win2D Canvas を再描画 colorPicker.ColorChanged += (s, e) => { win2DCanvas.Invalidate(); }; SizeChanged += (s, e) => { double edgeLength = Math.Min(ActualWidth / 2.0, ActualHeight); viewBox1.Width = edgeLength * 2; viewBox1.Height = edgeLength; }; const string ClockPath1 = "Assets/ClockPanel.png"; const string ClockPath2 = "Assets/ClockPanel1.png"; image1.Source = ClockPath1; image1.Tapped += (s, e) => { if (image1.Source == ClockPath1) { image1.Source = ClockPath2; } else { image1.Source = ClockPath1; } }; } }
/// <summary> /// Scale image to fill the whole window /// </summary> public static Transform2DEffect Fill(CanvasBitmap source) { float scale = MathF.Max((float)(MainPage.bounds.X / source.Size.Width), (float)(MainPage.bounds.Y / source.Size.Height)); return(ScaleImage(source, scale, scale)); }
public void ProcessFrame(ProcessVideoFrameContext context) { //using (var input = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface)) //using (var output = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface)) //using (var ds = output.CreateDrawingSession()) //{ // var time = context.InputFrame.RelativeTime ?? new TimeSpan(); // ds.Clear(Colors.Black); // for (uint i = 0; i < _numColumns; i++) // { // for (uint j = 0; j < _numRows; j++) // { // _crops[i, j].Source = input; // float scale = Rescale((float)(Math.Cos(time.TotalSeconds * 2f + 0.2f * (i + j))), 0.6f, 0.95f); // float rotation = (float)time.TotalSeconds * 1.5f + 0.2f * (i + j); // Vector2 centerPoint = new Vector2((i + 0.5f) * PixelsPerTile, (j + 0.5f) * PixelsPerTile); // _transforms[i, j].TransformMatrix = // Matrix3x2.CreateRotation(rotation, centerPoint) * // Matrix3x2.CreateScale(scale, centerPoint); // ds.DrawImage(_transforms[i, j]); // } // } //} // When using SupportedMemoryTypes => MediaMemoryTypes.GpuAndCpu we need to check if we're using GPU or CPU for the frame // If we're on GPU, use InputFrame.Direct3DSurface if (context.InputFrame.SoftwareBitmap == null) { using (var inputBitmap = CanvasBitmap.CreateFromDirect3D11Surface(_canvasDevice, context.InputFrame.Direct3DSurface)) using (var renderTarget = CanvasRenderTarget.CreateFromDirect3D11Surface(_canvasDevice, context.OutputFrame.Direct3DSurface)) using (var ds = renderTarget.CreateDrawingSession()) { var time = context.InputFrame.RelativeTime ?? new TimeSpan(); ds.Clear(Colors.Black); for (uint i = 0; i < _numColumns; i++) { for (uint j = 0; j < _numRows; j++) { _crops[i, j].Source = inputBitmap; float scale = Rescale((float)(Math.Cos(time.TotalSeconds * 2f + 0.2f * (i + j))), 0.6f, 0.95f); float rotation = (float)time.TotalSeconds * 1.5f + 0.2f * (i + j); Vector2 centerPoint = new Vector2((i + 0.5f) * PixelsPerTile, (j + 0.5f) * PixelsPerTile); _transforms[i, j].TransformMatrix = Matrix3x2.CreateRotation(rotation, centerPoint) * Matrix3x2.CreateScale(scale, centerPoint); ds.DrawImage(_transforms[i, j]); } } } return; } // If we're on CPU, use InputFrame.SoftwareBitmap if (context.InputFrame.Direct3DSurface == null) { // InputFrame's raw pixels byte[] inputFrameBytes = new byte[4 * context.InputFrame.SoftwareBitmap.PixelWidth * context.InputFrame.SoftwareBitmap.PixelHeight]; context.InputFrame.SoftwareBitmap.CopyToBuffer(inputFrameBytes.AsBuffer()); using (var inputBitmap = CanvasBitmap.CreateFromBytes( _canvasDevice, inputFrameBytes, context.InputFrame.SoftwareBitmap.PixelWidth, context.InputFrame.SoftwareBitmap.PixelHeight, context.InputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat())) using (var renderTarget = new CanvasRenderTarget( _canvasDevice, context.OutputFrame.SoftwareBitmap.PixelWidth, context.OutputFrame.SoftwareBitmap.PixelHeight, (float)context.OutputFrame.SoftwareBitmap.DpiX, context.OutputFrame.SoftwareBitmap.BitmapPixelFormat.ToDirectXPixelFormat(), CanvasAlphaMode.Premultiplied)) { using (var ds = renderTarget.CreateDrawingSession()) { var time = context.InputFrame.RelativeTime ?? new TimeSpan(); ds.Clear(Colors.Black); for (uint i = 0; i < _numColumns; i++) { for (uint j = 0; j < _numRows; j++) { _crops[i, j].Source = inputBitmap; float scale = Rescale((float)(Math.Cos(time.TotalSeconds * 2f + 0.2f * (i + j))), 0.6f, 0.95f); float rotation = (float)time.TotalSeconds * 1.5f + 0.2f * (i + j); Vector2 centerPoint = new Vector2((i + 0.5f) * PixelsPerTile, (j + 0.5f) * PixelsPerTile); _transforms[i, j].TransformMatrix = Matrix3x2.CreateRotation(rotation, centerPoint) * Matrix3x2.CreateScale(scale, centerPoint); ds.DrawImage(_transforms[i, j]); } } } } } }
public static SpriteSheet FromSingleSpriteBitmap(CanvasBitmap bitmap) { return(FromBitmap(bitmap, new Vector2((float)bitmap.Size.Width, (float)bitmap.Size.Height), Vector2.Zero)); }
public void setBallImage(CanvasBitmap ballImage) { ball.ballImage = ballImage; }
public static SpriteSheet FromBitmap(CanvasBitmap bitmap, Vector2 spriteSize, Vector2 origin) { return(new SpriteSheet(null, bitmap, spriteSize, origin)); }
async Task CaptureThumbnailFromCustomSource(IRandomAccessStream customThumbnail) { var bitmap = await CanvasBitmap.LoadAsync(new CanvasDevice(), customThumbnail); await SaveThumbnails(bitmap); }
public static async Task <SpriteSheet> FromBitmapUriAsync(ICanvasResourceCreator resourceCreator, Uri uri, Vector2 spriteSize, Vector2 origin) { var bitmap = await CanvasBitmap.LoadAsync(resourceCreator, uri); return(new SpriteSheet(uri, bitmap, spriteSize, origin)); }
public async Task Initialize(ICanvasResourceCreator cac) { Bmp = await CanvasBitmap.LoadAsync(cac, new Uri(Src)); Loaded = true; }
/// <summary> /// Loads a <see cref="CompositionBrush"/> from the input <see cref="System.Uri"/>, and prepares it to be used in a tile effect /// </summary> /// <param name="canvasDevice">The device to use to process the Win2D image</param> /// <param name="compositor">The compositor instance to use to create the final brush</param> /// <param name="uri">The path to the image to load</param> /// <param name="dpiMode">Indicates the desired DPI mode to use when loading the image</param> /// <returns>A <see cref="Task{T}"/> that returns the loaded <see cref="CompositionBrush"/> instance</returns> private static async Task <CompositionBrush> LoadSurfaceBrushAsync( CanvasDevice canvasDevice, Compositor compositor, Uri uri, DpiMode dpiMode) { var displayInformation = DisplayInformation.GetForCurrentView(); float dpi = displayInformation.LogicalDpi; // Load the bitmap with the appropriate settings using CanvasBitmap bitmap = dpiMode switch { DpiMode.UseSourceDpi => await CanvasBitmap.LoadAsync(canvasDevice, uri), DpiMode.Default96Dpi => await CanvasBitmap.LoadAsync(canvasDevice, uri, 96), DpiMode.DisplayDpi => await CanvasBitmap.LoadAsync(canvasDevice, uri, dpi), DpiMode.DisplayDpiWith96AsLowerBound => await CanvasBitmap.LoadAsync(canvasDevice, uri, dpi >= 96?dpi : 96), _ => throw new ArgumentOutOfRangeException(nameof(dpiMode), dpiMode, $"Invalid DPI mode: {dpiMode}") }; // Calculate the surface size Size size = bitmap.Size, sizeInPixels = new Size(bitmap.SizeInPixels.Width, bitmap.SizeInPixels.Height); // Get the device and the target surface using CompositionGraphicsDevice graphicsDevice = CanvasComposition.CreateCompositionGraphicsDevice(compositor, canvasDevice); // Create the drawing surface var drawingSurface = graphicsDevice.CreateDrawingSurface( sizeInPixels, DirectXPixelFormat.B8G8R8A8UIntNormalized, DirectXAlphaMode.Premultiplied); // Create a drawing session for the target surface using (var drawingSession = CanvasComposition.CreateDrawingSession(drawingSurface, new Rect(0, 0, sizeInPixels.Width, sizeInPixels.Height), dpi)) { // Fill the target surface drawingSession.Clear(Color.FromArgb(0, 0, 0, 0)); drawingSession.DrawImage(bitmap, new Rect(0, 0, size.Width, size.Height), new Rect(0, 0, size.Width, size.Height)); drawingSession.EffectTileSize = new BitmapSize { Width = (uint)size.Width, Height = (uint)size.Height }; } // Setup the effect brush to use var surfaceBrush = compositor.CreateSurfaceBrush(drawingSurface); surfaceBrush.Stretch = CompositionStretch.None; double pixels = displayInformation.RawPixelsPerViewPixel; // Adjust the scale if the DPI scaling is greater than 100% if (pixels > 1) { surfaceBrush.Scale = new Vector2((float)(1 / pixels)); surfaceBrush.BitmapInterpolationMode = CompositionBitmapInterpolationMode.NearestNeighbor; } return(surfaceBrush); } }
static CanvasBitmap MakeThumbnailPretty(CanvasBitmap capturedBitmap, float thumbnailWidth, float thumbnailHeight, Rect targetRect) { var pixelColors = capturedBitmap.GetPixelColors(); // Remove any unused space around the edge of the bitmap, so it will fill the thumbnail. Rect cropRect = CropCapturedBitmap(capturedBitmap, pixelColors); // Choose a (hopefully) aesthetically pleasing background color. Color backgroundColor = ChooseBackgroundColor(pixelColors); // Apply letterbox scaling to fit the image into the target thumbnail. Vector2 outputSize = new Vector2((float)targetRect.Width, (float)targetRect.Height); var sourceSize = new Vector2((float)cropRect.Width, (float)cropRect.Height); var letterbox = Utils.GetDisplayTransform(outputSize, sourceSize); var translate = Matrix3x2.CreateTranslation((float)targetRect.X, (float)targetRect.Y); // Position the image where we want it. var scaledImage = new Transform2DEffect { Source = new AtlasEffect { Source = capturedBitmap, SourceRectangle = cropRect, }, InterpolationMode = CanvasImageInterpolation.HighQualityCubic, TransformMatrix = letterbox * translate, }; // Create the final thumbnail image. var finalImage = new CompositeEffect { Sources = { // Blurred shadow. new ShadowEffect { Source = new MorphologyEffect { Source = scaledImage, Mode = MorphologyEffectMode.Dilate, Width = dilateAmount, Height = dilateAmount, }, BlurAmount = blurAmount, }, // Overlay the image itself. scaledImage } }; // Rasterize the effect into a rendertarget. CanvasRenderTarget output = new CanvasRenderTarget(capturedBitmap.Device, thumbnailWidth, thumbnailHeight, 96); using (var ds = output.CreateDrawingSession()) { ds.Clear(backgroundColor); ds.DrawImage(finalImage); } return(output); }
private async void ExportImage() { CanvasDevice device = CanvasDevice.GetSharedDevice(); using (CanvasRenderTarget offscreen = new CanvasRenderTarget( device, item.ImageSource.PixelWidth, item.ImageSource.PixelHeight, 96)) { using (IRandomAccessStream stream = await item.ImageFile.OpenReadAsync()) using (CanvasBitmap image = await CanvasBitmap.LoadAsync(offscreen, stream, 96)) { saturationEffect.Source = image; using (CanvasDrawingSession ds = offscreen.CreateDrawingSession()) { ds.Clear(Windows.UI.Colors.Black); // Need to copy the value of each effect setting. contrastEffect.Contrast = item.Contrast; exposureEffect.Exposure = item.Exposure; temperatureAndTintEffect.Temperature = item.Temperature; temperatureAndTintEffect.Tint = item.Tint; saturationEffect.Saturation = item.Saturation; graphicsEffect.BlurAmount = item.Blur; ds.DrawImage(graphicsEffect); } var fileSavePicker = new FileSavePicker() { SuggestedSaveFile = item.ImageFile }; fileSavePicker.FileTypeChoices.Add("JPEG files", new List <string>() { ".jpg" }); var outputFile = await fileSavePicker.PickSaveFileAsync(); if (outputFile != null) { using (IRandomAccessStream outStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) { await offscreen.SaveAsync(outStream, CanvasBitmapFileFormat.Jpeg); } ResetEffects(); var newItem = await MainPage.LoadImageInfo(outputFile); if (outputFile.Path == item.ImageFile.Path) { item.ImageSource = newItem.ImageSource; } else { item = newItem; } MainImage.Source = item.ImageSource; } } } }