How can i convert Bitmap to memory stream?
Asked Answered
U

2

7

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 ?

Unclear answered 6/10, 2014 at 1:40 Comment(2)
Is TextureLoader part of a library, and if so, which library?Militiaman
TextureLoader is part of Microsoft DirectX.Direct3D and i'm using DirectX 9 version in this project.Unclear
L
6

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)

Least answered 6/10, 2014 at 3:39 Comment(0)
P
4

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

Proofread answered 3/8, 2022 at 4:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.