Advertisement
Guest User

Untitled

a guest
May 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. private static Bitmap ConvertTo(byte[] data)
  2. {
  3. int pixelCount = (data.Length / 4) + 2;
  4. int l = (int)Math.Sqrt(pixelCount) + 1;
  5. int k = 0;
  6. Bitmap b = new Bitmap(l, l);
  7. byte[] n = new byte[l * l * 4];
  8. Buffer.BlockCopy(BitConverter.GetBytes(data.Length), 0, n, 0, 4);
  9. Buffer.BlockCopy(data, 0, n, 4, data.Length);
  10. for (int x = 0; x < l; x++)
  11. {
  12. for (int y = 0; y < l; y++)
  13. {
  14. int argb = BitConverter.ToInt32(n, k);
  15. k += 4;
  16. Color c = Color.FromArgb(argb/*n[k++], n[k++], n[k++], n[k++]*/);
  17. b.SetPixel(x, y, c);
  18. }
  19. }
  20. return b;
  21. }
  22.  
  23. private static byte[] ConvertFromBmp(Bitmap b)
  24. {
  25. int l = b.Width;
  26. int n = l * l * 4;
  27. byte[] buff = new byte[n];
  28. int k = 0;
  29. for (int x = 0; x < l; x++)
  30. {
  31. for (int y = 0; y < l; y++)
  32. {
  33. Buffer.BlockCopy(BitConverter.GetBytes(b.GetPixel(x, y).ToArgb()), 0, buff, k, 4);
  34. k += 4;
  35. }
  36. }
  37. int len = BitConverter.ToInt32(buff, 0);
  38. byte[] f = new byte[len];
  39. Buffer.BlockCopy(buff, 4, f, 0, f.Length);
  40. return f;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement