[DirectX C#] TextureLoader.FromStream throw exception Index was outside the bounds of the array.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all!
Currently I creating a game now, and I have a problem when loading Textures from the stream.
The texture is using Bitmap Image, and the Bitmap source files is packed and compressed in 1 file using my own file format.
My program was running correctly to extracting the Bitmap files, the extracted file give me the correct image without any invalid pixels.
but when I save the bytes into the stream and load them to the Texture using TextureLoader, it give me an exception "Index was outside the bounds of the array." and here is my code (the XDS class is contains Header Information of Texture, including
Transparency Color in argb):
<pre class="prettyprint // These 2 function give me exception "Index was outside the bounds of the array."

public static Texture CreateTexture(MemoryStream sourcestream)
{
Bitmap file = new Bitmap(sourcestream);
sourcestream = new MemoryStream();

file.Save(sourcestream, System.Drawing.Imaging.ImageFormat.Bmp);
return TextureLoader.FromStream(Config.Renderer.d3dev, sourcestream, file.Width, file.Height, 0, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, Color.White.ToArgb());
}

public static Texture CreateTexture(MemoryStream sourcestream, XDS mainheader)
{
Bitmap file = new Bitmap(sourcestream);
file.MakeTransparent(Color.FromArgb(mainheader.TransparencyCode));
sourcestream = new MemoryStream();

file.Save(sourcestream, System.Drawing.Imaging.ImageFormat.Bmp);
return TextureLoader.FromStream(Config.Renderer.d3dev, sourcestream);
}






// and this one give me Error in Application
public static Texture CreateTexture(MemoryStream sourcestream)
{
Bitmap file = new Bitmap(sourcestream);
sourcestream = new MemoryStream();

file.Save(sourcestream, System.Drawing.Imaging.ImageFormat.Bmp);
return TextureLoader.FromStream(Config.Renderer.d3dev, sourcestream, (int)sourcestream.Length, file.Width, file.Height, 0, Usage.None, Format.Unknown, Pool.Managed, Filter.None, Filter.None, Color.White.ToArgb());
}[/code]
<br/>
I also already tried this, but the result width and height isnt 100% (for example, my bitmap width and height is 800x600 it give me about 780x560)

<pre class="prettyprint public static Texture CreateTexture(Bitmap sourceimage, XDS mainheader)
{
sourceimage.MakeTransparent(Color.FromArgb(mainheader.TransparencyCode));
return new Texture(Config.Renderer.d3dev, sourceimage, 0, Pool.Managed);
}[/code]
<br/>
I dont know what I must to do to solve this.
I hope anyone can help me..

Sorry if my English so bad.
Thanks.

View the full article
 
Back
Top