SFML community forums

Bindings - other languages => DotNet => Topic started by: sofakng on February 10, 2009, 03:17:57 pm

Title: How can I convert SFML.Graphics.Image to System.Drawing.Imag
Post by: sofakng on February 10, 2009, 03:17:57 pm
I'm drawing to load a bunch of images using SFML.Graphics.Image and then display them in a System.Windows.Forms.ImageList object.

However, this object requires images to be System.Drawing.Image.

How can I convert from SFML.Graphics.Image to System.Drawing.Image?

Thanks for any help!
Title: How can I convert SFML.Graphics.Image to System.Drawing.Imag
Post by: Laurent on February 10, 2009, 04:33:59 pm
You can access the image pixels as a two-dimensional byte array, then I guess you can build your System.Drawing.Image from that.
Title: How can I convert SFML.Graphics.Image to System.Drawing.Imag
Post by: sofakng on February 10, 2009, 05:25:29 pm
Yeah, I noticed SFML.Graphics.Image.Pixels() but I can't seem to figure out how to store it into a System.Drawing.Image.Bitmap (which is apparantly what I need to do).  You can't instantiate a System.Drawing.Image directly so I'm guessing I need a bitmap...
Title: How can I convert SFML.Graphics.Image to System.Drawing.Imag
Post by: Laurent on February 10, 2009, 05:49:05 pm
Maybe this can help?
http://www.codeproject.com/KB/recipes/ImageConverter.aspx
Title: How can I convert SFML.Graphics.Image to System.Drawing.Imag
Post by: renner96 on July 31, 2009, 09:11:44 pm
Is there a way to do the opposite???

(see my other post: http://www.sfml-dev.org/forum/viewtopic.php?p=9436#9436)
Title: ReĀ : How can I convert SFML.Graphics.Image to System.Drawing.Imag
Post by: Martin on November 02, 2015, 05:15:32 pm
Hello. I realize that this is an old topic. But given that I found this topic when looking for an answer, I thought others might also.

Here is the solution I came up with:

using SFML.Graphics;
using System;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public static class ImageExtensions
{
    public static System.Drawing.Bitmap ConvertToBitmap(this Image image)
    {
        var bitmap = new System.Drawing.Bitmap((int)image.Size.X, (int)image.Size.Y, PixelFormat.Format32bppArgb);
        var bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
            ImageLockMode.WriteOnly, bitmap.PixelFormat);
        byte[] data = getPixelData(image);
        Marshal.Copy(data, 0, bitmapData.Scan0, data.Length);
        bitmap.UnlockBits(bitmapData);
        return bitmap;
    }

    private static byte[] getPixelData(Image image)
    {
        byte[] data = image.Pixels;

        // SFML is always RGBA, regardless of the endian-ness of the CPU. On a little-endian
        // CPU, System.Drawing.Bitmap expects BGRA.
        if (BitConverter.IsLittleEndian)
        {
            byte[] source = image.Pixels;
            data = new byte[source.Length];
            for (int i = 0; i < data.Length; i += 4)
            {
                data[i] = source[i + 2];
                data[i + 1] = source[i + 1];
                data[i + 2] = source[i];
                data[i + 3] = source[i + 3];
            }
        }
        return data;
    }
}

(The above code is free for anyone to use and adapt, without restriction, for any purpose)