C # Convert a byte stream to an instance method of a picture _c# tutorial

Source: Internet
Author: User
Tags instance method

Copy Code code as follows:

Usingsystem;
UsingSystem.Collections.Generic;
Usingsystem.linq;
Usingsystem.text;
usingsystem.drawing;
Usingsystem.io;
NamespaceMicrosoft.Form.Base
{
Classimagetobyte
{
<summary>
Picture converted into byte stream
</summary>
<param name= "img" > Image object to convert </param>
<returns> returns the byte stream after conversion </returns>
Publicstaticbyte[] IMGTOBYT (Image img)
{
MemoryStream ms = Newmemorystream ();
byte[] ImageData = null;
Img. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg);
ImageData = Ms. GetBuffer ();
Returnimagedata;
}
<summary>
Byte flow change to Picture
</summary>
<param name= "byt" > Byte stream to convert </param>
<returns> transform to get Image object </returns>
Publicstaticimage byttoimg (byte[] byt)
{
MemoryStream ms = Newmemorystream (byt);
Image img = image.fromstream (ms);
returnimg;
}
//
<summary>
Returns the text stream of a picture according to the picture path byte[]
</summary>
<param name= "ImagePath" > Picture path </param>
<returns> returns the byte stream </returns>
Privatestaticbyte[] Getimagebyte (Stringimagepath)
{
FileStream files = Newfilestream (ImagePath, FileMode.Open);
byte[] Imgbyte = newbyte[files. Length];
Files. Read (imgbyte, 0, imgbyte.length);
Files. Close ();
Returnimgbyte;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.