Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void LoadTexturesCpu(this ContentManager contentManager, List<string> assetName, Action<List<Texture2D>> action)
- {
- lock (Threading.BackgroundContext)
- {
- ThreadPool.QueueUserWorkItem((s) =>
- {
- IGraphicsDeviceService serv = contentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
- List<Texture2D> assets = new List<Texture2D>();
- //load assets
- for (int i = 0; i < assetName.Count; i++)
- {
- BitmapEncoder encoder = new JpegBitmapEncoder();
- BitmapImage myBitmapImage = new BitmapImage();
- using (MemoryStream ms = new MemoryStream())
- {
- myBitmapImage.BeginInit();
- myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Koala.jpg");
- myBitmapImage.EndInit();
- //add frame to encoder and save it to the stream
- encoder.Frames.Add(BitmapFrame.Create(myBitmapImage));
- encoder.Save(ms);
- //byte[] data = ms.ToArray();
- // Texture2D texture = Texture2D.FromStream(serv.GraphicsDevice, ms);
- //texture.Name = assetName[i];
- using (Bitmap image = (Bitmap)Bitmap.FromStream(ms))
- {
- // Fix up the Image to match the expected format
- image.RGBToBGR();
- var data = new byte[image.Width * image.Height * 4];
- BitmapData bitmapData = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
- ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
- if (bitmapData.Stride != image.Width * 4) throw new NotImplementedException();
- Marshal.Copy(bitmapData.Scan0, data, 0, data.Length);
- image.UnlockBits(bitmapData);
- Texture2D texture = null;
- texture = new Texture2D(serv.GraphicsDevice, image.Width, image.Height);
- texture.SetData(data);
- assets.Add(BuildMipMapCPU(texture, data));
- }
- }
- }
- //report done
- if (action != null)
- {
- Dispatcher.CurrentDispatcher.Invoke(action, assets);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment