简体   繁体   中英

How can i convert Bitmap to memory stream?

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?

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)

I get below code from here: http://www.java2s.com/example/csharp/system.drawing/bitmap-to-memory-stream.html

public static MemoryStream ToMemoryStream(this Bitmap b)
        {
            MemoryStream ms = new MemoryStream();
            b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms;
        }

Work for my need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM