Page 1 of 2

BitmapSource to BitmapImage in Noesis

Posted: 07 Feb 2025, 11:57
by MarioBarbero
I am triying to do a Loading with sprites and I dont know how to do this with Noesis.
This method throw many errors when I execute it in Noesis.
        private BitmapImage ConvertToBitmapImage(BitmapSource bitmapSource)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
                encoder.Save(memoryStream);
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
                bitmapImage.EndInit();
                return bitmapImage;
            }
        }

Re: BitmapSource to BitmapImage in Noesis

Posted: 07 Feb 2025, 12:41
by jsantos
Is this in Unity? Please use the TAGS in your new posts to indicate this.

Re: BitmapSource to BitmapImage in Noesis

Posted: 07 Feb 2025, 12:56
by MarioBarbero
Yes, First, I doint that in WPF, without UNity and without Noesis. Then, I write the code in Unity and throw many errors.

I want to do an animated loading with sprites. I have all sprite in one image, the same image.
The code is: in the attached files. I am attaching it as a zip because it won't let me attach the files separately.

Re: BitmapSource to BitmapImage in Noesis

Posted: 07 Feb 2025, 14:04
by jsantos
Are you using native Sprites and atlases in Unity? That's the way to go in Unity, using the sprites as standard textures.

Re: BitmapSource to BitmapImage in Noesis

Posted: 07 Feb 2025, 14:48
by MarioBarbero
Maybe I explained myself wrong. I have the xaml views associated with Unity. I'm not doing it directly in Unity.
So I want to do the Loading in a xaml.
I have seen the example that you do here (https://www.noesisengine.com/docs/Gui.C ... orial.html), but I have all the sprites in a single image, like the Aladin image in the example, but is it necessary to create an animation for each sprite? In my case,
I have 36 sprites, in 6 rows of 6 each. Total 36.
Isn't there some way to create an animation for everything and have it rotate? As in the example that I attached in the previous message.

Re: BitmapSource to BitmapImage in Noesis

Posted: 10 Feb 2025, 12:47
by jsantos
Are you using native Sprites and atlases in Unity? Because this is the way to go in Unity.

Please have a look at the Inventory sample included in the package.

Re: BitmapSource to BitmapImage in Noesis

Posted: 10 Feb 2025, 13:55
by MarioBarbero
But can't it be done in WPF and then call the component, in this case a UserControl that will have this Loading inside?

When you say look at the example, do you mean the example of the Aladin images?

Re: BitmapSource to BitmapImage in Noesis

Posted: 10 Feb 2025, 14:09
by jsantos
In our Unity package, there is a sample called Inventory that uses sprites.

One advantage of Unity sprites is that you can design your XAML using separate images (e.g., sprite0.png, sprite1.png, sprite2.png, etc.). Then, as a completely separate step independent of the design process, you can combine all the sprites into a single atlas that is used at runtime, without doing any changes to the XAML.

Re: BitmapSource to BitmapImage in Noesis

Posted: 10 Feb 2025, 14:11
by jsantos
This approach is inefficient and not well-suited for Unity:
bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());
A better approach in Unity is to use image assets, as they undergo an offline process to convert them into optimized textures with mipmaps and compression.

Unity sprites allow this.

Re: BitmapSource to BitmapImage in Noesis

Posted: 10 Feb 2025, 17:36
by MarioBarbero
Thank you so much.
I followed the example you told me and
I managed to make the animation for lack of one thing: I want to paint it yellow, but if I do, nothing comes out. Originally the logo file is blank. It would be painting it yellow in code.

How can I set the color yellow?

I attach the config of sprite and the video with Atlas sprites (https://drive.google.com/file/d/18kg-l3 ... sp=sharing). It won't let me attach the video.

The code:
        private void LoadFrames()
        {
            Uri p = SpinnerLogoPath.UriSource;
            BitmapImage spriteSheet = new BitmapImage(p);

            _frames = new List<ImageSource>();
            for (int i = 0; i < 36; i++)
            {
                int x = (i % 6) * 512;
                int y = (i / 6) * 512;
                ImageSource s = new CroppedBitmap(spriteSheet, new Int32Rect(x, y, 512, 512));

                _frames.Add(s);
                UnityEngine.Debug.Log(s);
            }
        }
        
        private void StartAnimationSpinner()
        {
            Storyboard storyboard = (Storyboard)FindResource("SpinnerAnimation");
            ObjectAnimationUsingKeyFrames animation = (ObjectAnimationUsingKeyFrames)storyboard.Children[0];

            TimeSpan frameDuration = TimeSpan.FromMilliseconds(50); // 50ms en cada frame
            TimeSpan currentTime = TimeSpan.Zero;

            foreach (var frame in _frames)
            {
                ObjectKeyFrame keyFrame = new Noesis.DiscreteObjectKeyFrame()
                {
                    Value = frame,
                    KeyTime = currentTime
                };
                animation.KeyFrames.Add(keyFrame);
                currentTime += frameDuration;
            }

            storyboard.Begin();
        }
        
    <UserControl.Resources>
        <Storyboard x:Key="SpinnerAnimation"
                    RepeatBehavior="Forever">
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SpinnerImage"
                                           Storyboard.TargetProperty="Source"
                                           RepeatBehavior="Forever">
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>