View Issue Details

IDProjectCategoryView StatusLast Update
0003212NoesisGUIC# SDKpublic2024-06-04 13:17
Reportervoula_d Assigned Tosfernandez  
PrioritynormalSeverityminor 
Status resolvedResolutionno change required 
Product Version3.2 
Target Version3.2.4Fixed in Version3.2.4 
Summary0003212: Bitmap image is not rendered correctly.
Description

I need to dynamically load image from a stream which in WPF I am doing by using the following:

BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CreateOptions = BitmapCreateOptions.None;
bi.StreamSource = stream;
bi.EndInit();

As the above code is not Noesis compatible, I am trying to do the same with the following code:
System.Drawing.Image img = System.Drawing.Image.FromFile("Images/Diagram_WhiteBackground.png");
byte[] arr;
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
arr = ms.ToArray();
Bitmap b = new Bitmap(ms);
ImgSource = BitmapImage.Create(b.Width, b.Height, b.HorizontalResolution, b.VerticalResolution, arr, b.Width * 3, BitmapSource.Format.RGBA8);
}

The code above occasionally fails with the error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt".
But even when it executes successfully it produces a blurred image.

Attached Files
noesis.png (207,634 bytes)   
noesis.png (207,634 bytes)   
wpf.png (25,802 bytes)   
wpf.png (25,802 bytes)   
NoesisTest.zip (95,471 bytes)
PlatformAny

Activities

sfernandez

sfernandez

2024-04-10 12:17

manager   ~0009363

When using BitmapSource.Create() you should provide the raw pixels of the image without any file header. And the format and stride should match (for example, Stride=Width*4 and Format=RGBA8).
Could you verify that the input to that method is correct?

voula_d

voula_d

2024-04-10 14:50

reporter   ~0009365

Thanks for the reply and guidance. I was able to make this work by changing the code per your suggestion:
BitmapData sourceData = b.LockBits(new System.Drawing.Rectangle(0, 0,
b.Width, b.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte[] sourceBuffer = new byte[sourceData.Stride * sourceData.Height];
Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, sourceBuffer.Length);
b.UnlockBits(sourceData);
ImgSource = BitmapImage.Create(b.Width, b.Height, b.HorizontalResolution, b.VerticalResolution, sourceBuffer, sourceData.Stride, BitmapSource.Format.BGRA8);
b.Dispose();

I saw other posts where you are suggesting that using a FileTextureProvider is the way to go when trying to dynamically load images and I maybe that approach would better fit our requirements. Is there a way to use multiple FileTextureProviders and decide which one to use depending on some criteria?

Thank you

sfernandez

sfernandez

2024-06-03 19:53

manager   ~0009618

Yes, you can have your own FileTextureProvider. For example, depending on the Uri format, or if the file is available or not, you can decide whether to use your custom implementation, or just fallback to any other implementation (similar to what we do in our EmbeddedTextureProvider: https://github.com/Noesis/Managed/blob/master/Src/NoesisApp/Core/Src/Core/EmbeddedTextureProvider.cs).

Regarding the issue about the BitmapSource, can we close this ticket?

voula_d

voula_d

2024-06-04 12:23

reporter   ~0009630

Yes, thank you. I found a way to render the bitmap so you can close the issue.

Issue History

Date Modified Username Field Change
2024-04-05 12:26 voula_d New Issue
2024-04-05 12:26 voula_d File Added: noesis.png
2024-04-05 12:26 voula_d File Added: wpf.png
2024-04-05 12:26 voula_d File Added: NoesisTest.zip
2024-04-10 11:52 sfernandez Assigned To => sfernandez
2024-04-10 11:52 sfernandez Status new => assigned
2024-04-10 11:52 sfernandez Target Version => 3.2.4
2024-04-10 11:52 sfernandez Description Updated
2024-04-10 11:52 sfernandez Description Updated
2024-04-10 12:17 sfernandez Status assigned => feedback
2024-04-10 12:17 sfernandez Note Added: 0009363
2024-04-10 14:50 voula_d Note Added: 0009365
2024-04-10 14:50 voula_d Status feedback => assigned
2024-06-03 19:53 sfernandez Status assigned => feedback
2024-06-03 19:53 sfernandez Note Added: 0009618
2024-06-04 12:23 voula_d Note Added: 0009630
2024-06-04 12:23 voula_d Status feedback => assigned
2024-06-04 13:17 sfernandez Status assigned => resolved
2024-06-04 13:17 sfernandez Resolution open => no change required
2024-06-04 13:17 sfernandez Fixed in Version => 3.2.4