java image bitmap_Image与Bitmap的区别及相互转换

///

///将图片Image转换成Byte[]///

/// image对象

/// 后缀名

///

public static byte[] ImageToBytes(Image Image, System.Drawing.Imaging.ImageFormat imageFormat)

{if (Image == null) { return null; }byte[] data = http://www.cnblogs.com/peasana/archive/2012/02/13/null;

using (MemoryStream ms= newMemoryStream())

{using (Bitmap Bitmap = newBitmap(Image))

{

Bitmap.Save(ms, imageFormat);

ms.Position= 0;

data= http://www.cnblogs.com/peasana/archive/2012/02/13/new byte[ms.Length];

ms.Read(data,0, Convert.ToInt32(ms.Length));

ms.Flush();

}

}returndata;

}///

///byte[]转换成Image///

/// 二进制图片流

/// Image

public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)

{if (byteArrayIn == null)return null;using (System.IO.MemoryStream ms = newSystem.IO.MemoryStream(byteArrayIn))

{

System.Drawing.Image returnImage=System.Drawing.Image.FromStream(ms);

ms.Flush();returnreturnImage;

}

}//Image转换Bitmap

1. Bitmap img = newBitmap(imgSelect.Image);2. Bitmap bmp =(Bitmap)pictureBox1.Image;//Bitmap转换成Image

usingSystem.IO;private staticSystem.Windows.Controls.Image Bitmap2Image(System.Drawing.Bitmap Bi)

{

MemoryStream ms= newMemoryStream();

Bi.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

BitmapImage bImage= newBitmapImage();

bImage.BeginInit();

bImage.StreamSource= newMemoryStream(ms.ToArray());

bImage.EndInit();

ms.Dispose();

Bi.Dispose();

System.Windows.Controls.Image i= newSystem.Windows.Controls.Image();

i.Source=bImage;returni ;

}//byte[] 转换 Bitmap

public static Bitmap BytesToBitmap(byte[] Bytes)

{

MemoryStream stream= null;try{

stream= newMemoryStream(Bytes);return new Bitmap((Image)newBitmap(stream));

}catch(ArgumentNullException ex)

{throwex;

}catch(ArgumentException ex)

{throwex;

}finally{

stream.Close();

}

}//Bitmap转byte[]

public static byte[] BitmapToBytes(Bitmap Bitmap)

{

MemoryStream ms= null;try{

ms= newMemoryStream();

Bitmap.Save(ms, Bitmap.RawFormat);byte[] byteImage = newByte[ms.Length];

byteImage=ms.ToArray();returnbyteImage;

}catch(ArgumentNullException ex)

{throwex;

}finally{

ms.Close();

}

}

你可能感兴趣的:(java,image,bitmap)