PDA

View Full Version : تبدیل Bitmap به [] byte



tthenry14plus
یک شنبه 18 فروردین 1387, 01:40 صبح
چطور میتونم عکس ذخیره شده در Bitmap رو بدون واسطه قرار دادن هارد دیسک به رشته بایتی ([]byte)تبدیل کنم ؟

hassan razavi
یک شنبه 18 فروردین 1387, 07:35 صبح
Bitmap bm = new Bitmap("d:\\1.bmp");
byte[] b=new byte[10000];
Stream s=null;
bm.Save(s, System.Drawing.Imaging.ImageFormat.Bmp);
s.Write(b, 0, b.Length);

naeeme
یک شنبه 18 فروردین 1387, 14:24 عصر
// Create a new bitmap.
Bitmap bmp = new Bitmap("c:\\Img.bmp");

// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData =
bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
// This code is specific to a bitmap with 24 bits per pixels.
int bytes = bmp.Width * bmp.Height * 3;
byte[] rgbValues = new byte[bytes];

// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

Amir Oveisi
یک شنبه 18 فروردین 1387, 14:28 عصر
میتونین از کلاس Imageconvertor استفاده کنید

tthenry14plus
دوشنبه 19 فروردین 1387, 01:33 صبح
من الان به #C دسترسی ندارم ولی فکر می کنم که
stream نمی تونه null باشه !!!

gdevnb
دوشنبه 19 فروردین 1387, 02:24 صبح
من الان به #C دسترسی ندارم ولی فکر می کنم که
stream نمی تونه null باشه !!!
من تست کردم مشکلی نداشت.

tthenry14plus
دوشنبه 19 فروردین 1387, 03:50 صبح
الان تستش کردم مشکل دارم
همون مشکلی که فکرشو می کردم.

Value cannot be null.

فکر میکنید مشکل از کجا باشه؟

gdevnb
دوشنبه 19 فروردین 1387, 04:53 صبح
ببخشید حق با شماست کد اول مشکل داشت ولی دومی درست کار میکنه.
در ضمن به جای استریم از MemoryStream استفاده کردم(کد اول). برنامه خطا نداشت ولی مقداری در بافر قرار نمیداد؟؟؟


MemoryStream s = new MemoryStream();

علیرضا مداح
دوشنبه 19 فروردین 1387, 07:37 صبح
سلام ،


public static byte[] GetBytes(Image image)
{
if (image == null) return null ;
ImageConverter lmageConverter = new ImageConverter();
return (byte[])lmageConverter.ConvertTo(image, Type.GetType("System.Byte[]"));
}