void OnMainPageLoaded(object sender, RoutedEventArgs args) { // Find the dimension of the square bitmap if (ApplicationView.Value == ApplicationViewState.FullScreenPortrait) { dimension = (int)(this.ActualWidth - referencePanel.Margin.Left - referencePanel.Margin.Right); } else { dimension = (int)(this.ActualHeight - referencePanel.Margin.Top - referencePanel.Margin.Bottom); } // Set this size to the reference panel so it doesn't get distorted in Snapped view referencePanel.Width = dimension; referencePanel.Height = dimension; // Create the SurfaceImageSource and renderer SurfaceImageSource surfaceImageSource = new SurfaceImageSource(dimension, dimension); surfaceImageSourceRenderer = new SurfaceImageSourceRenderer(surfaceImageSource, dimension, dimension); image.Source = surfaceImageSource; // Set rotation centers rotate.CenterX = dimension / 2; rotate.CenterY = dimension / 2; inverseRotate.CenterX = dimension / 2; inverseRotate.CenterY = dimension / 2; // Start the event CompositionTarget.Rendering += OnCompositionTargetRendering; }
/// <summary> /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance. /// </summary> /// <param name="pixelWidth">Width of the target in pixels</param> /// <param name="pixelHeight">Height of the target in pixels</param> public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight, bool supportOpacity = false) { this.pixelWidth = pixelWidth; this.pixelHeight = pixelHeight; this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight, supportOpacity); surfaceImageSourceNative = Collect(ComObject.As <SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource)); }
/// <summary> /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance. /// </summary> /// <param name="pixelWidth">Width of the target in pixels</param> /// <param name="pixelHeight">Height of the target in pixels</param> public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight) { this.pixelWidth = pixelWidth; this.pixelHeight = pixelHeight; this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight); surfaceImageSourceNative = ComObject.As <SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource); }
async void MainPage_Loaded(object sender, RoutedEventArgs e) { // locate the image var imageUri = new Uri("ms-appx:///Assets/GcLogo.png"); var storageFile = await StorageFile.GetFileFromApplicationUriAsync(imageUri); // load the image into C1Bitmap _bitmap = new C1Bitmap(); await _bitmap.LoadAsync(storageFile, new FormatConverter(PixelFormat.Format32bppPBGRA)); // create Direct2D and DirectWrite factories _d2dFactory = D2D.Factory2.Create(D2D.FactoryType.SingleThreaded); _dwFactory = DW.Factory.Create(DW.FactoryType.Shared); // create GPU resources CreateDeviceResources(); // create the image source _imageSource = new SurfaceImageSource(_marginLT + _bitmap.PixelWidth + _marginRB, _marginLT + _bitmap.PixelHeight + _marginRB, false); // obtain the native interface for the image source _sisNative = ComObject.QueryInterface <DXGI.ISurfaceImageSourceNative>(_imageSource); _sisNative.SetDevice(_dxgiDevice); // draw the image to SurfaceImageSource UpdateImageSource(ImageEffect.Original); // associate the image source with the Image image.Source = _imageSource; }
void OnClearButtonClick(object sender, RoutedEventArgs e) { SurfaceImageSource surfaceImageSource = new SurfaceImageSource(dimension, dimension); surfaceImageSourceRenderer = new SurfaceImageSourceRenderer(surfaceImageSource, dimension, dimension); image.Source = surfaceImageSource; }
/// <summary> /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance. /// </summary> /// <param name="pixelWidth">Width of the target in pixels</param> /// <param name="pixelHeight">Height of the target in pixels</param> public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight, bool supportOpacity = false) { this.pixelWidth = pixelWidth; this.pixelHeight = pixelHeight; this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight, supportOpacity); surfaceImageSourceNative = ToDispose(ComObject.As <SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource)); viewDatas[0] = new SurfaceViewData(); viewDatas[1] = new SurfaceViewData(); }
/// <summary> /// Initializes a new <see cref="SurfaceImageSourceTarget" /> instance. /// </summary> /// <param name="pixelWidth">Width of the target in pixels</param> /// <param name="pixelHeight">Height of the target in pixels</param> public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight, bool supportOpacity = false) { this._pixelWidth = pixelWidth; this._pixelHeight = pixelHeight; _surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight, supportOpacity); _surfaceImageSourceNative = Collect(ComObject.As <ISurfaceImageSourceNative>(_surfaceImageSource)); _viewDatas[0] = Collect(new SurfaceViewData()); _viewDatas[1] = Collect(new SurfaceViewData()); }
/// <summary> /// Initialzes a new <see cref="SurfaceImageSourceTarget"/> instance. /// </summary> /// <param name="pixelWidth">Width of the target in pixels</param> /// <param name="pixelHeight">Height of the target in pixels</param> /// <param name="isOpaque">Indicates whether the surface is expected to be always opaque (without transparency support).</param> public SurfaceImageSourceTarget(int pixelWidth, int pixelHeight, bool isOpaque = false) { this.pixelWidth = pixelWidth; this.pixelHeight = pixelHeight; this.surfaceImageSource = new SurfaceImageSource(pixelWidth, pixelHeight, isOpaque); surfaceImageSourceNative = ComObject.As <SharpDX.DXGI.ISurfaceImageSourceNative>(surfaceImageSource); viewDatas[0] = new SurfaceViewData(); viewDatas[1] = new SurfaceViewData(); }
protected override async void OnNavigatedTo(NavigationEventArgs e) { DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape; // // Doing all that video processing is too much for low-end phones like the Lumia 520 // Pick-and-choose which piece should run // VideoPreview.MediaFailed += VideoPreview_MediaFailed; //var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/video.cvmpilj.mjpg")); //var stream = await file.OpenAsync(FileAccessMode.Read); //var source = await HttpMjpegCaptureSource.CreateFromStreamAsync(stream, "myboundary"); var source = await HttpMjpegCaptureSource.CreateFromUriAsync("http://216.123.238.208/axis-cgi/mjpg/video.cgi?camera&resolution=640x480"); VideoPreview.SetMediaStreamSource(source.Source); var settings = new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video }; await settings.SelectVideoDeviceAsync(VideoDeviceSelection.BackOrFirst); _capture = new MediaCapture(); await _capture.InitializeAsync(settings); var graphicsDevice = MediaGraphicsDevice.CreateFromMediaCapture(_capture); var previewProps = (VideoEncodingProperties)_capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); TextLog.Text += String.Format("Preview: {0} {1}x{2} {3}fps\n", previewProps.Subtype, previewProps.Width, previewProps.Height, previewProps.FrameRate.Numerator / (float)previewProps.FrameRate.Denominator); TextLog.Text += "Creating MediaSamplePresenter from SurfaceImageSource\n"; var image = new SurfaceImageSource((int)previewProps.Width, (int)previewProps.Height); ImagePreview.Source = image; _imagePresenter = ImagePresenter.CreateFromSurfaceImageSource(image, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height); TextLog.Text += "Creating MediaSamplePresenter from SwapChainPanel\n"; _swapChainPresenter = ImagePresenter.CreateFromSwapChainPanel( SwapChainPreview, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height ); TextLog.Text += "Creating MediaReader\n"; _mediaReader = await MediaReader.CreateFromMediaCaptureAsync(_capture, AudioInitialization.Deselected, VideoInitialization.Bgra8); TextLog.Text += "Starting video loop\n"; var ignore = Task.Run(() => VideoLoop()); }
void ClearImageSource() { if (_sisNative != null) { image.Source = null; _sisNative.Dispose(); _sisNative = null; _imageSource = null; } }
public SurfaceImageSourceCanvas(SurfaceImageSource surfaceImageSource, Rect updateRect, Direct2DFactories factories = null) : base(factories) { sisn = ComObject.As <DXGI.ISurfaceImageSourceNative> (surfaceImageSource); SharpDX.Point offset; var surface = sisn.BeginDraw(updateRect.ToRectangle(), out offset); var dpi = 96.0; var properties = new D2D1.RenderTargetProperties(D2D1.RenderTargetType.Default, new D2D1.PixelFormat(DXGI.Format.Unknown, D2D1.AlphaMode.Unknown), (float)(dpi), (float)(dpi), D2D1.RenderTargetUsage.None, D2D1.FeatureLevel.Level_DEFAULT); Initialize(new D2D1.RenderTarget(this.factories.D2DFactory, surface, properties)); }
public void CS_WP_N_Basic() { ExecuteOnUIThread(async() => { var capture = new MediaCapture(); await capture.InitializeAsync(new MediaCaptureInitializationSettings { StreamingCaptureMode = StreamingCaptureMode.Video }); var graphicsDevice = MediaGraphicsDevice.CreateFromMediaCapture(capture); var previewProps = (VideoEncodingProperties)capture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview); var image = new SurfaceImageSource((int)previewProps.Width, (int)previewProps.Height); var imagePresenter = ImagePresenter.CreateFromSurfaceImageSource( image, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height ); var panel = new SwapChainPanel(); var swapChainPresenter = ImagePresenter.CreateFromSwapChainPanel( panel, graphicsDevice, (int)previewProps.Width, (int)previewProps.Height ); var readerProps = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Bgra8, previewProps.Width, previewProps.Height); readerProps.FrameRate.Numerator = previewProps.FrameRate.Numerator; readerProps.FrameRate.Denominator = previewProps.FrameRate.Denominator; var captureReader = await CaptureReader.CreateAsync( capture, new MediaEncodingProfile { Video = readerProps }); using (MediaSample2D sample = (MediaSample2D)await captureReader.GetVideoSampleAsync()) { swapChainPresenter.Present(sample); imagePresenter.Present(sample); var folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("MediaCaptureReaderTests", CreationCollisionOption.OpenIfExists); var file = await folder.CreateFileAsync("CS_WP_N_Basic.jpg", CreationCollisionOption.ReplaceExisting); await sample.SaveToFileAsync(file, ImageCompression.Jpeg); } }); }
void CreateImageSource() { ClearImageSource(); _imageSource = new SurfaceImageSource(_bitmap.PixelWidth, _bitmap.PixelHeight, false); _sisNative = ComObject.QueryInterface <DXGI.ISurfaceImageSourceNative>(_imageSource); _sisNative.SetDevice(_dxgiDevice); UpdateImageSource(false); image.Source = _imageSource; }
private async void button_Click(object sender, RoutedEventArgs e) { var picker = new Windows.Storage.Pickers.FileOpenPicker(); picker.FileTypeFilter.Add(".jpg"); var file = await picker.PickSingleFileAsync(); Lumia.Imaging.BitmapRenderer renderer = new Lumia.Imaging.BitmapRenderer(); renderer.ColorMode = Lumia.Imaging.ColorMode.Yuv420P; renderer.Source = new Lumia.Imaging.StorageFileImageSource(file); var bmp = await renderer.RenderAsync(); var surfaceImageSource = new SurfaceImageSource((int)bmp.Dimensions.Width, (int)bmp.Dimensions.Height); var yuvRenderer = YuvRenderer.YuvD3DRenderer.CreateForSwapChainPanel(root, (uint)bmp.Dimensions.Width, (uint)bmp.Dimensions.Height, YuvRenderer.StretchMode.UniformToFill); Task.Run(() => { //// convert to nv12. //var yReader = DataReader.FromBuffer(bmp.Buffers[0].Buffer); //var uReader = DataReader.FromBuffer(bmp.Buffers[1].Buffer); //var vReader = DataReader.FromBuffer(bmp.Buffers[2].Buffer); //byte[] yData = new byte[bmp.Buffers[0].Buffer.Length]; //byte[] uData = new byte[bmp.Buffers[1].Buffer.Length]; //byte[] vData = new byte[bmp.Buffers[2].Buffer.Length]; //yReader.ReadBytes(yData); //uReader.ReadBytes(uData); //vReader.ReadBytes(vData); //var height = (int)(bmp.Dimensions.Height); //var halfWidth = ((int)(bmp.Dimensions.Width)) / 2; //var halfHeight = height / 2; //var heightAndHalf = height + halfHeight; //var nv12Data = new byte[bmp.Buffers[0].Pitch * heightAndHalf]; //Array.Copy(yData, nv12Data, yData.Length); //Parallel.For(0, halfHeight, y => //{ // var firstPixelOfRow = yData.Length + y * bmp.Buffers[0].Pitch; // for (int x = 0; x < halfWidth; ++x) // { // nv12Data[x * 2 + firstPixelOfRow] = uData[y * bmp.Buffers[1].Pitch + x]; // nv12Data[x * 2 + firstPixelOfRow + 1] = vData[y * bmp.Buffers[2].Pitch + x]; // } //}); while (true) { yuvRenderer.RenderYuv420P(bmp.Buffers[0].Buffer, bmp.Buffers[1].Buffer, bmp.Buffers[2].Buffer, bmp.Buffers[0].Pitch, bmp.Buffers[1].Pitch); } }); }
internal async void ContinuePick(StorageFile file) { Lumia.Imaging.BitmapRenderer renderer = new Lumia.Imaging.BitmapRenderer(); renderer.ColorMode = Lumia.Imaging.ColorMode.Yuv420P; renderer.Source = new Lumia.Imaging.StorageFileImageSource(file); var bmp = await renderer.RenderAsync(); var surfaceImageSource = new SurfaceImageSource((int)bmp.Dimensions.Width, (int)bmp.Dimensions.Height); var yuvRenderer = YuvRenderer.YuvD3DRenderer.CreateForSwapChainPanel(root, (uint)bmp.Dimensions.Width, (uint)bmp.Dimensions.Height, YuvRenderer.StretchMode.Uniform); Task.Run(() => { // convert to nv12. var yReader = DataReader.FromBuffer(bmp.Buffers[0].Buffer); var uReader = DataReader.FromBuffer(bmp.Buffers[1].Buffer); var vReader = DataReader.FromBuffer(bmp.Buffers[2].Buffer); byte[] yData = new byte[bmp.Buffers[0].Buffer.Length]; byte[] uData = new byte[bmp.Buffers[1].Buffer.Length]; byte[] vData = new byte[bmp.Buffers[2].Buffer.Length]; yReader.ReadBytes(yData); uReader.ReadBytes(uData); vReader.ReadBytes(vData); var height = (int)(bmp.Dimensions.Height); var halfWidth = ((int)(bmp.Dimensions.Width)) / 2; var halfHeight = height / 2; var heightAndHalf = height + halfHeight; var nv12Data = new byte[bmp.Buffers[0].Pitch * heightAndHalf]; Array.Copy(yData, nv12Data, yData.Length); for (int y = 0; y < halfHeight; ++y) { var firstPixelOfRow = yData.Length + y * bmp.Buffers[0].Pitch; for (int x = 0; x < halfWidth; ++x) { nv12Data[x * 2 + firstPixelOfRow] = uData[y * bmp.Buffers[1].Pitch + x]; nv12Data[x * 2 + firstPixelOfRow + 1] = vData[y * bmp.Buffers[2].Pitch + x]; } } while (true) { yuvRenderer.RenderNV12(nv12Data, bmp.Buffers[0].Pitch); } }); }
protected override void Dispose(bool disposeManagedResources) { this.surfaceImageSource = null; if (surfaceImageSourceNative != null) { surfaceImageSourceNative.Dispose(); surfaceImageSourceNative = null; } for (int i = 0; i < viewDatas.Length; i++) { viewDatas[i].Dispose(); viewDatas[i] = null; } base.Dispose(disposeManagedResources); }
private void DirectXRender(List <UIElement> points) { if (points.Count <= 0) { return; } Line initLine = points[0] as Line; Point p1 = new Point ( Math.Min(initLine.X1 - initLine.StrokeThickness / 2, initLine.X2 - initLine.StrokeThickness / 2), Math.Min(initLine.Y1 - initLine.StrokeThickness / 2, initLine.Y2 - initLine.StrokeThickness / 2) ), p2 = new Point ( Math.Max(initLine.X1 + initLine.StrokeThickness / 2, initLine.X2 + initLine.StrokeThickness / 2), Math.Max(initLine.Y1 + initLine.StrokeThickness / 2, initLine.Y2 + initLine.StrokeThickness / 2) ); foreach (var child in points) { var line = child as Line; if (line == null) { continue; } if (p1.X > line.X1 - line.StrokeThickness / 2) { p1.X = line.X1 - line.StrokeThickness / 2; } if (p1.X > line.X2 - line.StrokeThickness / 2) { p1.X = line.X2 - line.StrokeThickness / 2; } if (p2.X < line.X1 + line.StrokeThickness / 2) { p2.X = line.X1 + line.StrokeThickness / 2; } if (p2.X < line.X2 + line.StrokeThickness / 2) { p2.X = line.X2 + line.StrokeThickness / 2; } if (p1.Y > line.Y1 - line.StrokeThickness / 2) { p1.Y = line.Y1 - line.StrokeThickness / 2; } if (p1.Y > line.Y2 - line.StrokeThickness / 2) { p1.Y = line.Y2 - line.StrokeThickness / 2; } if (p2.Y < line.Y1 + line.StrokeThickness / 2) { p2.Y = line.Y1 + line.StrokeThickness / 2; } if (p2.Y < line.Y2 + line.StrokeThickness / 2) { p2.Y = line.Y2 + line.StrokeThickness / 2; } } var bndRect = new Rect(p1, p2); var dxTarget = new SurfaceImageSource ( (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1), (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1) ); SharpDX.DXGI.ISurfaceImageSourceNative dxTargetNative = SharpDX.ComObject.As <SharpDX.DXGI.ISurfaceImageSourceNative>(dxTarget); dxTargetNative.Device = d3dDevice.QueryInterface <SharpDX.DXGI.Device>(); /* * Draw Logic */ SharpDX.DrawingPoint drawingPoint; var surface = dxTargetNative.BeginDraw(new SharpDX.Rectangle(0, 0, (int)(bndRect.Width * DisplayProperties.LogicalDpi / 96.0 + 1), (int)(bndRect.Height * DisplayProperties.LogicalDpi / 96.0 + 1)), out drawingPoint); var dxRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface, new SharpDX.Direct2D1.RenderTargetProperties() { DpiX = DisplayProperties.LogicalDpi, DpiY = DisplayProperties.LogicalDpi, PixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), Type = SharpDX.Direct2D1.RenderTargetType.Default, Usage = SharpDX.Direct2D1.RenderTargetUsage.None }); dxRenderTarget.BeginDraw(); dxRenderTarget.Clear(SharpDX.Color.Transparent); foreach (var child in points) { var line = child as Line; if (line == null) { continue; } Color c = (line.Stroke as SolidColorBrush).Color; var brush = new SharpDX.Direct2D1.SolidColorBrush(dxRenderTarget, new SharpDX.Color(c.R, c.G, c.B, c.A)); var style = new SharpDX.Direct2D1.StrokeStyleProperties(); style.LineJoin = SharpDX.Direct2D1.LineJoin.Round; style.StartCap = SharpDX.Direct2D1.CapStyle.Round; style.EndCap = SharpDX.Direct2D1.CapStyle.Round; var stroke = new SharpDX.Direct2D1.StrokeStyle(d2dFactory, style); dxRenderTarget.DrawLine( new SharpDX.DrawingPointF((float)(line.X1 - bndRect.Left), (float)(line.Y1 - bndRect.Top)), new SharpDX.DrawingPointF((float)(line.X2 - bndRect.Left), (float)(line.Y2 - bndRect.Top)), brush, (float)line.StrokeThickness, stroke); } dxRenderTarget.EndDraw(); dxTargetNative.EndDraw(); var dxImage = new Image(); dxImage.Source = dxTarget; canvas.Children.Add(dxImage); Canvas.SetLeft(dxImage, bndRect.X); Canvas.SetTop(dxImage, bndRect.Y); }