public static byte[] BitmapToByteArray(Bitmap bitmap) { ByteBuffer byteBuffer = ByteBuffer.Allocate(bitmap.ByteCount); bitmap.CopyPixelsToBuffer(byteBuffer); byte[] bytes = byteBuffer.ToArray<byte>(); return bytes; }
private static void CacheImage(string url, Bitmap bitmap) { string imagePath = GetProfileImagePath(url); if (!File.Exists(imagePath)) { ByteBuffer byteBuffer = ByteBuffer.Allocate(bitmap.ByteCount); bitmap.CopyPixelsToBuffer(byteBuffer); MemoryStream ms = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 100, ms); byte[] bytes = ms.ToBytes(); using (var fs = File.Create(imagePath)) { fs.Write(bytes,0,bytes.Length); } } }