Functional Requirements
1. Convert a picture (PNG bmp JPEG bmp GIF) to a byte array and manipulate it in memory.
2. Converts an in-memory byte array into an Image object and assigns a value to the corresponding control display.
3, from the picture byte array to obtain the corresponding picture format, generates a picture to save to the disk.
The Image of this is System.Drawing.Image.
Get an image from fileimage image = Image.FromFile ("d:\\test.jpg"); Bitmap Bitmap = new Bitmap ("d:\\test.jpg");
Here are three functions that implement each of the three requirements:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Drawing.Imaging; Using system.drawing;using system.io;namespace netutilitylib{public class Imagehelper {//<summary> Convert Image to byte[]//</summary>//<param name= "Image" ></param> <returns></returns> public static byte[] Imagetobytes (image image) {ImageFormat Format = image. Rawformat; using (MemoryStream ms = new MemoryStream ()) {//Complex point of Judgment//if (format. Equals (imageformat.jpeg))//{//image. Save (MS, Imageformat.jpeg); }//else if (format. Equals (imageformat.png))//{//image. Save (MS, Imageformat.png); }//else if (format. Equals (imageformat.bmp))//{//image. Save(MS, Imageformat.bmp); }//else if (format. Equals (imageformat.gif))//{//image. Save (MS, Imageformat.gif); }//else if (format. Equals (Imageformat.icon))//{//image. Save (MS, Imageformat.icon); }//The image is directly handled. Save (MS, format); byte[] buffer = new Byte[ms. Length]; Image.Save () changes the position of MemoryStream and requires a re-seek to the 0 position Ms where begin begins. Seek (0, Seekorigin.begin); Ms. Read (buffer, 0, buffer. Length); return buffer; }//using}//imagetobytes//<summary>//Convert byte[] to Image//</summary> <param name= "bytes" ></param>///<returns></returns> public static Image Bytestoimage (byte[] buffer) {MemoryStream ms = new MemoryStream (buFfer); Image image = Image.fromstream (ms); return image; }//bytestoimage//<summary>//Convert byte[] to a picture and Store it in File//</sum mary>//<param name= "FileName" ></param>///<param name= "buffer" ></param> <returns></returns> public static string Createimagefrombytes (String fileName, byte[] buffer) {//Get the picture's format image image = Bytestoimage (buffer); ImageFormat format = image. Rawformat; Depending on the format, set the suffix if (format). Equals (Imageformat.jpeg)) {fileName + = ". Jpeg"; } else if (format. Equals (imageformat.png)) {fileName + = ". Png"; } else if (format. Equals (imageformat.bmp)) {fileName + = ". Bmp"; } else if (format. Equals (imageformat.gif)) { FileName + = ". gif"; } else if (format. Equals (Imageformat.icon)) {fileName + = ". Icon"; }//Create file directory FileInfo info = new FileInfo (fileName); Directory.CreateDirectory (info. Directory.fullname); Writes the array to the specified position file.writeallbytes (fileName, buffer); return fileName; }//createimagefrombytes}}
Do some introductions
Stream Flow Class
A stream is an abstraction of a sequence of bytes that may come from a file, TCP/IP socket, or memory. In. NET, the stream is represented appropriately by the Stream class. The Stream class provides a common view of the sequence of bytes.
The stream class is the base class for all other streams and has the following class implementations:
- BufferedStream: Provides a buffer layer on the other stream to improve performance
- FileStream: Provides a way to read and write files
- MemoryStream: Provides a stream that uses memory as a fallback memory
- NetworkStream: Provides a way to access data on the network
- CryptoStream: Provides a way to supply encrypted transform data
This blog is mainly used in MemoryStream, so only the introduction of it, the other in the reference article link.
Sometimes it is necessary to manipulate the data in memory, rather than to take the method of saving the data in a file, the corresponding example is the Pictruebox control in WindowsForm. For example, you have a picture that appears in the PictureBox control, and you want to send the picture to a remote server, such as a web Service.picturebox control that has a save () method that allows you to save the image to a stream object.
Instead of saving the image to a FileStream object and then reloading the data in the file into a byte array, a better approach would be to use the MemoryStream object, which uses memory as a fallback memory (more efficient than performing file I/O, using memory; file i/ O execution speed is relatively slow).
Image.Save method
Image.Save method (Stream, ImageFormat)
This method saves the image to the specified stream in the specified format. The image must be saved in a zero offset stream. If any additional data has been written to the stream before the image is saved, the image data in the stream is corrupted.
Resources
Stream Flow class Link: http://blog.csdn.net/gengkunpeng/article/details/5797231
[C # Reference]byte array and image conversion