Skip to main content
added 118 characters in body
Source Link
David Gouveia
  • 25k
  • 5
  • 89
  • 127
public static class TextureLoader
{
    public static Texture2D FromFile(GraphicsDevice device, string path, Microsoft.Xna.Framework.Rectangle? sourceRectangle = null)
    {
        // XNA 4.0 removed FromFile in favor of FromStream
        // So if we don't pass a source rectangle just delegate to FromStream
        if(sourceRectangle == null)
        {
            return Texture2D.FromStream(device, new FileStream(path, FileMode.Open));
        }
        else
        {
            // If we passed in a source rectangle convert it to System.Drawing.Rectangle
            Microsoft.Xna.Framework.Rectangle xnaRectangle = sourceRectangle.Value;
            System.Drawing.Rectangle gdiRectangle = new System.Drawing.Rectangle(xnaRectangle.X, xnaRectangle.Y, xnaRectangle.Width, xnaRectangle.Height);
            
            // Start by loading the entire image
            Image image = Image.FromFile(path);

            // Create an empty bitmap to contain our crop
            Bitmap bitmap = new Bitmap(gdiRectangle.Width, gdiRectangle.Height, image.PixelFormat);
            
            // Draw the cropped image region to the bitmap
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), gdiRectangle.X, gdiRectangle.Y, gdiRectangle.Width, gdiRectangle.Height, GraphicsUnit.Pixel);
            
            // Save the bitmap into a memory stream
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Png);
            
            // Finally create the Texture2D from stream
            Texture2D texture = Texture2D.FromStream(device, stream);
            
            // Clean up
            stream.Dispose();
            graphics.Dispose();
            bitmap.Dispose();
            image.Dispose();

            return texture;
        }
    }
}
public static class TextureLoader
{
    public static Texture2D FromFile(GraphicsDevice device, string path, Microsoft.Xna.Framework.Rectangle? sourceRectangle = null)
    {
        // XNA 4.0 removed FromFile in favor of FromStream
        // So if we don't pass a source rectangle just delegate to FromStream
        if(sourceRectangle == null)
        {
            return Texture2D.FromStream(device, new FileStream(path, FileMode.Open));
        }
        else
        {
            // If we passed in a source rectangle convert it to System.Drawing.Rectangle
            Microsoft.Xna.Framework.Rectangle xnaRectangle = sourceRectangle.Value;
            System.Drawing.Rectangle gdiRectangle = new System.Drawing.Rectangle(xnaRectangle.X, xnaRectangle.Y, xnaRectangle.Width, xnaRectangle.Height);
            
            // Start by loading the entire image
            Image image = Image.FromFile(path);

            // Create an empty bitmap to contain our crop
            Bitmap bitmap = new Bitmap(gdiRectangle.Width, gdiRectangle.Height, image.PixelFormat);
            
            // Draw the cropped image region to the bitmap
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.DrawImage(image, 0, 0, gdiRectangle, GraphicsUnit.Pixel);
            
            // Save the bitmap into a memory stream
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Png);
            
            // Finally create the Texture2D from stream
            Texture2D texture = Texture2D.FromStream(device, stream);
            
            // Clean up
            stream.Dispose();
            graphics.Dispose();
            bitmap.Dispose();
            image.Dispose();

            return texture;
        }
    }
}
public static class TextureLoader
{
    public static Texture2D FromFile(GraphicsDevice device, string path, Microsoft.Xna.Framework.Rectangle? sourceRectangle = null)
    {
        // XNA 4.0 removed FromFile in favor of FromStream
        // So if we don't pass a source rectangle just delegate to FromStream
        if(sourceRectangle == null)
        {
            return Texture2D.FromStream(device, new FileStream(path, FileMode.Open));
        }
        else
        {
            // If we passed in a source rectangle convert it to System.Drawing.Rectangle
            Microsoft.Xna.Framework.Rectangle xnaRectangle = sourceRectangle.Value;
            System.Drawing.Rectangle gdiRectangle = new System.Drawing.Rectangle(xnaRectangle.X, xnaRectangle.Y, xnaRectangle.Width, xnaRectangle.Height);
            
            // Start by loading the entire image
            Image image = Image.FromFile(path);

            // Create an empty bitmap to contain our crop
            Bitmap bitmap = new Bitmap(gdiRectangle.Width, gdiRectangle.Height, image.PixelFormat);
            
            // Draw the cropped image region to the bitmap
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), gdiRectangle.X, gdiRectangle.Y, gdiRectangle.Width, gdiRectangle.Height, GraphicsUnit.Pixel);
            
            // Save the bitmap into a memory stream
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Png);
            
            // Finally create the Texture2D from stream
            Texture2D texture = Texture2D.FromStream(device, stream);
            
            // Clean up
            stream.Dispose();
            graphics.Dispose();
            bitmap.Dispose();
            image.Dispose();

            return texture;
        }
    }
}
added 219 characters in body
Source Link
David Gouveia
  • 25k
  • 5
  • 89
  • 127

And the best part is that since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast too.And the best part is that since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast too. Okay this is not true! You still have to do the premultiply alpha calculations. I just didn't remember well since I already had code to do that in place.

But still wondering if there'sBut still wondering if there's a way to do this directly from the Stream without having to pass through GDI. Mind wandered towards decorating the Stream in some way but didn't arrive anywhere. :)

And from browsing through my old code I found a waycomment about Texture2D.FromStream being bugged and not being able to do this directly fromread all of the Stream without having to pass throughformats the documentation says it does, so I've been using GDI anyway. Mind wandered towards decorating the Stream in some way but didn't arrive anywhereSo I'll be staying with this method for now. :)

And the best part is that since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast too. Okay this is not true! You still have to do the premultiply alpha calculations. I just didn't remember well since I already had code to do that in place.

But still wondering if there's a way to do this directly from the Stream without having to pass through GDI. Mind wandered towards decorating the Stream in some way but didn't arrive anywhere. :)

And the best part is that since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast too. Okay this is not true! You still have to do the premultiply alpha calculations. I just didn't remember well since I already had code to do that in place.

But still wondering if there's a way to do this directly from the Stream without having to pass through GDI. Mind wandered towards decorating the Stream in some way but didn't arrive anywhere. :)

And from browsing through my old code I found a comment about Texture2D.FromStream being bugged and not being able to read all of the formats the documentation says it does, so I've been using GDI anyway. So I'll be staying with this method for now.

added 219 characters in body
Source Link
David Gouveia
  • 25k
  • 5
  • 89
  • 127

And the best part is thatAnd the best part is that since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast too. Okay this is not true! You still have to do the premultiply alpha calculations. I just didn't remember well since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast tooI already had code to do that in place.

And the best part is that since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast too.

And the best part is that since at the end it still relies on Texture2D.FromStream, the whole premultiplied alpha phase is automated! Runs relatively fast too. Okay this is not true! You still have to do the premultiply alpha calculations. I just didn't remember well since I already had code to do that in place.

Source Link
David Gouveia
  • 25k
  • 5
  • 89
  • 127
Loading