Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How can I convert SFML.Graphics.Image to System.Drawing.Imag  (Read 6920 times)

0 Members and 1 Guest are viewing this topic.

sofakng

  • Newbie
  • *
  • Posts: 18
    • View Profile
How can I convert SFML.Graphics.Image to System.Drawing.Imag
« 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!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How can I convert SFML.Graphics.Image to System.Drawing.Imag
« Reply #1 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.
Laurent Gomila - SFML developer

sofakng

  • Newbie
  • *
  • Posts: 18
    • View Profile
How can I convert SFML.Graphics.Image to System.Drawing.Imag
« Reply #2 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...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

renner96

  • Newbie
  • *
  • Posts: 3
    • View Profile
How can I convert SFML.Graphics.Image to System.Drawing.Imag
« Reply #4 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)

Martin

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Weaving Stories
ReĀ : How can I convert SFML.Graphics.Image to System.Drawing.Imag
« Reply #5 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)