Guest User

Untitled

a guest
Mar 28th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. public static void LoadTexturesCpu(this ContentManager contentManager, List<string> assetName, Action<List<Texture2D>> action)
  2. {
  3. lock (Threading.BackgroundContext)
  4. {
  5. ThreadPool.QueueUserWorkItem((s) =>
  6. {
  7. IGraphicsDeviceService serv = contentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
  8.  
  9. List<Texture2D> assets = new List<Texture2D>();
  10. //load assets
  11.  
  12. for (int i = 0; i < assetName.Count; i++)
  13. {
  14.  
  15. BitmapEncoder encoder = new JpegBitmapEncoder();
  16. BitmapImage myBitmapImage = new BitmapImage();
  17. using (MemoryStream ms = new MemoryStream())
  18. {
  19. myBitmapImage.BeginInit();
  20. myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Koala.jpg");
  21. myBitmapImage.EndInit();
  22.  
  23. //add frame to encoder and save it to the stream
  24. encoder.Frames.Add(BitmapFrame.Create(myBitmapImage));
  25. encoder.Save(ms);
  26.  
  27. //byte[] data = ms.ToArray();
  28. // Texture2D texture = Texture2D.FromStream(serv.GraphicsDevice, ms);
  29.  
  30.  
  31. //texture.Name = assetName[i];
  32.  
  33. using (Bitmap image = (Bitmap)Bitmap.FromStream(ms))
  34. {
  35. // Fix up the Image to match the expected format
  36. image.RGBToBGR();
  37.  
  38. var data = new byte[image.Width * image.Height * 4];
  39.  
  40. BitmapData bitmapData = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
  41. ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  42. if (bitmapData.Stride != image.Width * 4) throw new NotImplementedException();
  43. Marshal.Copy(bitmapData.Scan0, data, 0, data.Length);
  44. image.UnlockBits(bitmapData);
  45.  
  46. Texture2D texture = null;
  47. texture = new Texture2D(serv.GraphicsDevice, image.Width, image.Height);
  48. texture.SetData(data);
  49.  
  50. assets.Add(BuildMipMapCPU(texture, data));
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. //report done
  58. if (action != null)
  59. {
  60. Dispatcher.CurrentDispatcher.Invoke(action, assets);
  61.  
  62. }
  63.  
  64. });
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment