endlesstravel / Love2dCS

C# Wrapper for LÖVE, a 2d game engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

implement conversion method for images and imagedata to System.Drawing.Bitmap

Shadowblitz16 opened this issue · comments

please implement ToBitmap() for love images and image data.
it would return a system.drawing.bitmap

commented

Here the System.Drawing.Bitmap does't support in .net standard 1.2 :
image
The System.Drawing.Bitmap have compatibility issues on cross platform, so i will not add them to Love2dCS library. Excuse me for the inconvenience .

But i will give you code to implement it and here is the code to do that :

using System.Windows.Forms;
using Love;

namespace Project1
{
    class Issue91 : Scene
    {
        /// <summary>
        /// convert Image to Bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap ToBitmap(Image image)
        {
            var canvas = Graphics.NewCanvas(image.GetWidth(), image.GetHeight());
            var oldTargets = Graphics.GetCanvas();
            Graphics.SetCanvas(canvas);
            Graphics.Draw(image);
            Graphics.SetCanvas(oldTargets);
            var imageData = canvas.NewImageData();
            return ToBitmap(imageData);
        }

        /// <summary>
        /// convert ImageData to Bitmap
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap ToBitmap(ImageData imageData)
        {
            var dataBytes = imageData.Encode(ImageFormat.PNG).GetBytes();
            var stream = new System.IO.MemoryStream(dataBytes);
            var bitmap = new System.Drawing.Bitmap(stream);
            return bitmap;
        }


        Form form = null;
        public override void Load()
        {
            // create image
            var imageData = Image.NewImageData(100, 100);
            imageData.MapPixel((x, y, c) =>
            {
                return new Color(x / 100f, y / 100f, 1, 1);
            });
            var bitmap1 = ToBitmap(imageData);
            imageData.MapPixel((x, y, c) =>
            {
                return new Color(x / 100f, 1, y / 100f, 1);
            });
            var bitmap2 = ToBitmap(Graphics.NewImage(imageData));

            // draw to form
            form =  new System.Windows.Forms.Form();
            form.Show();
            var g = form.CreateGraphics();
            g.DrawImage(bitmap1, new System.Drawing.PointF());
            g.DrawImage(bitmap2, new System.Drawing.PointF(100, 100));
        }


    }
}

result here:
image

ah ok thankyou for the info

commented

close by it finished.