I created a BitMapSource from a list of RGBA pixels:
BitmapSource bmp = BitmapSource.Create(imageStrideInPixels, height, 96, 96, PixelFormats.Bgra32, null, imageData, imageStrideInPixels * pixelWidth);
Then create an image from BitMapSource:
// create image and set image as source Image BmpImg = new Image(); BmpImg.SetValue(Canvas.ZIndexProperty, 0); BmpImg.Width = imageScaleWidth; BmpImg.Height = imageScaleHeight; BmpImg.Source = bmp;
Then I add the image to the canvas:
mycanvas.Width = imageScaleWidth; mycanvas.Height = imageScaleHeight; mycanvas.Children.Clear(); mycanvas.Children.Add(BmpImg); Canvas.SetLeft(BmpImg, 0); // to set position (x,y) Canvas.SetTop(BmpImg, 0);
The problem is that it does not get scaled to imageScaleWidth and imageScaleHeight, and it is displayed halfway down the canvas.
Notice I was able to do this in Java SWT:
imageData = imageData.scaledTo(imageScaleWidth, imageScaleHeight); gc.drawImage(imageData, 0, 0);
source share