How I can quickly draw into texture with alpha? C#
How I can quickly draw into texture with alpha?
From MSDN -
Surface.GetGtaphics()
TheGetGraphics method is valid only on the following formats:Format.R5G6B5,Format.X1R5G5B5,Format.R8G8B8,and
Format.X8R8G8B8.Formats that contain
alphaare not supported
because the Microsoft Windows Graphics Device Interface (GDI)implementations do not have a well-defined behavior on the alpha channel. For
more information about formats, see
Format.
[1122 byte] By [
Chort] at [2007-12-25]
I did it !!!
Texture texture = new Texture( device, 256, 256, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
GraphicsStream graphicsStream = texture.LockRectangle(0, LockFlags.None);
Bitmap bitmap = new Bitmap( 256, 256, 256*4, PixelFormat.Format32bppPArgb, graphicsStream .InternalData );
Graphics g = Graphics.FromImage( bitmap );
...
texture.UnlockRectangle(0);
Here is a full example to load a bitmap and copy to a texture.
(There still is some scaling issue I will look at but it should help most people out)
Good luck.
// load a bitmap (I want to copy this to the texture
using (Bitmap bitmap = new Bitmap(filename))
{
_texture = new Texture(Control.Device,
bitmap.Width, bitmap.Height, 1, Usage.None, Format.A8R8G8B8,
Pool.Managed);
GraphicsStream graphicsStream = _texture.LockRectangle(0,
LockFlags.None);
Bitmap bmp = new Bitmap(bitmap.Width, bitmap.Height, bitmap.Width * 4,
PixelFormat.Format32bppPArgb, graphicsStream.InternalData);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(bitmap, Point.Empty);
_texture.UnlockRectangle(0);
bmp.Dispose();
}
OK, after some more messing about. This works 100% on my machine.
Load the bitmap.
Create a texture using the bitmap size + bug fix.
Create another bitmap attached to the texture bitstream.
Use the temp bitmap (attached to the texture's unmanaged data) and draw to that bitmap.
(remeber DrawImage() might scale if you don't specify the Units).
Dispose of all the goodies.
And there you go.
using (Bitmap fileBitmap = new Bitmap(filename))
{
Size size = new Size(fileBitmap.Width + 1, fileBitmap.Height + 1); //
FIX: texture seems to have some nasty bug where the size is not used
properly
_texture = new Texture(Control.Device, size.Width, size.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
using(GraphicsStream graphicsStream = _texture.LockRectangle(0, LockFlags.None))
using(Bitmap texBitmap = new Bitmap(size.Width, size.Height, size.Width
* 4, PixelFormat.Format32bppPArgb, graphicsStream.InternalData))
using (Graphics texGraphics = Graphics.FromImage(texBitmap))
{
// draw the image using it's pixel measurement. (Not DPI, Texture DPI not always the same as image DPI)
Rectangle rect = new Rectangle(Point.Empty, fileBitmap.Size);
texGraphics.DrawImage(fileBitmap, rect, rect, GraphicsUnit.Pixel);
}
_texture.UnlockRectangle(0);