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,

andFormat.X8R8G8B8.

Formats that containalpha

are 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, seeFormat.

[1122 byte] By [Chort] at [2007-12-25]
# 1
You can do it by rendering pimitives forming the shape you want with the alpha vlaue you specify to their colors. GetGraphics() will give you back a GDI graphics object which isn't capable of filling the alpha channel (not to mention that it's usually not a good solution in terms of performance).
WessamBahnassi at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 2
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);

Chort at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 3
Are you rendering to that texture? If so could you post a full example, I have not been able to successfully render to a texture using surfaces.
Equis at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 4
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();

}

LeslieGodwin at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 5
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);

LeslieGodwin at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...