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