How can i convert Bitmap to memory stream?

Daniel Bout picture Daniel Bout · Oct 6, 2014 · Viewed 7.2k times · Source

I have a code in form1 constructor:

ConvertedBmp = ConvertTo24(newest.FullName);

The function ConvertTo24 is:

private static Bitmap ConvertTo24(string inputFileName)
        {
            sw = Stopwatch.StartNew();
            Bitmap bmpIn = (Bitmap)Bitmap.FromFile(inputFileName);
            Bitmap converted = new Bitmap(bmpIn.Width, bmpIn.Height, PixelFormat.Format24bppRgb);
            using (Graphics g = Graphics.FromImage(converted))
            {
                g.PageUnit = GraphicsUnit.Pixel;
                g.DrawImageUnscaled(bmpIn, 0, 0);
            }
            sw.Stop();
            return converted;
        }

The problem is how can i use the ConvertedBmp in this line:

backTexture = TextureLoader.FromFile(D3Ddev, @"D:\test.bmp");

TextureLoader have some properties and two of them are: Fromfile and it's getting device and string or FromStream and it's getting device and Stream.

I have the device object already but how can i use the ConvertedBmp(Bitmap type) with the TextureLoader ?

Answer

Peter Kiss picture Peter Kiss · Oct 6, 2014

Bitmap class has a method called Save() which accepts a Stream (for example a MemoryStream object) and an ImageFormat, use that. After saved the Bitmap into a MemoryStream you can use that with TextureLoader.

Image.Save Method (Stream, ImageFormat)