C # Get Image Image format

Source: Internet
Author: User

Private voidButton1_click_1 (Objectsender, EventArgs e) {            stringPath =Application.startuppath; using(FileStream file =NewFileStream (Txtpath. Text, FileMode.Open)) {Image img=image.fromstream (file); stringformat; System.Drawing.Imaging.ImageFormat _img_format= Getimageformat (IMG, outformat); if(_img_format! =NULL) {img. Save (Path+@"\"+ Guid.NewGuid (). ToString () +format, _img_format); MessageBox.Show ("OK"); }            }                    }        /// <summary>        ///Get image Image format/// </summary>        /// <param name= "file" ></param>        /// <param name= "format" ></param>        /// <returns></returns>        PrivateSystem.Drawing.Imaging.ImageFormat Getimageformat (FileStream file, out stringformat) {            byte[] sb =New byte[2];//This reads the position length of the direct 0-1.File. Read (SB,0, sb.            Length); //Judging by the file header            stringStrflag = sb[0]. ToString () + sb[1].            ToString (); //View format Type            Switch(strflag) {//jpg format                 Case"255216": Format=". jpg"; returnSystem.Drawing.Imaging.ImageFormat.Jpeg; //gif format                 Case"7173": Format=". gif"; returnSystem.Drawing.Imaging.ImageFormat.Gif; //BMP Format                 Case"6677": Format=". bmp"; returnSystem.Drawing.Imaging.ImageFormat.Bmp; //PNG format                 Case"13780": Format=". PNG"; returnSystem.Drawing.Imaging.ImageFormat.Png; //Other formats                default: Format=string.                    Empty; return NULL; }        }        /// <summary>        ///Get image Image format/// </summary>        /// <param name= "_img" ></param>        /// <param name= "format" ></param>        /// <returns></returns>        PrivateSystem.Drawing.Imaging.ImageFormat Getimageformat (Image _img, out stringformat) {            if(_img. Rawformat.equals (System.Drawing.Imaging.ImageFormat.Jpeg)) {format=". jpg"; returnSystem.Drawing.Imaging.ImageFormat.Jpeg; }            if(_img. Rawformat.equals (System.Drawing.Imaging.ImageFormat.Gif)) {format=". gif"; returnSystem.Drawing.Imaging.ImageFormat.Gif; }            if(_img. Rawformat.equals (System.Drawing.Imaging.ImageFormat.Png)) {format=". PNG"; returnSystem.Drawing.Imaging.ImageFormat.Png; }            if(_img. Rawformat.equals (SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.BMP)) {format=". bmp"; returnSystem.Drawing.Imaging.ImageFormat.Bmp; } format=string.            Empty; return NULL; }

C # Get Image Image format

Related Article

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.