c#图片下载的标准写法

MemoryStream的介绍:http://www.cnblogs.com/JimmyZheng/archive/2012/04/14/2446507.html

bitmap.Save(strOutputPathJpg, System.Drawing.Imaging.ImageFormat.Png);
using (Bitmap bmp = new Bitmap(@"D:\map\v10-841-142-1357879384609.jpg")) { ImageFormat format = bmp.RawFormat; Console.WriteLine(format); Console.WriteLine(); } 虽然扩展名为jpg 但确实是PNG 判断依据是:Guid互相对应。。。这里的GUID是怎么规定的 how why

下一次再试一次。
bmp Guid = {b96b3cab-0728-11d3-9d7b-0000f81ef32e}
emf  Guid = {b96b3cac-0728-11d3-9d7b-0000f81ef32e}
exif   Guid = {b96b3cb2-0728-11d3-9d7b-0000f81ef32e}
gif    Guid = {b96b3cb0-0728-11d3-9d7b-0000f81ef32e}
Guid = {b96b3cb5-0728-11d3-9d7b-0000f81ef32e} icon
Guid = {b96b3cae-0728-11d3-9d7b-0000f81ef32e} jepg
Guid = {b96b3caa-0728-11d3-9d7b-0000f81ef32e} memorymap
Guid = {b96b3caf-0728-11d3-9d7b-0000f81ef32e} png
Guid = {b96b3cb1-0728-11d3-9d7b-0000f81ef32e} tiff
Guid = {b96b3cad-0728-11d3-9d7b-0000f81ef32e} wmf

c#图片下载的标准写法

图片下载的标准写法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace cn.tianditu.crawl.tile.core
{
    public class HttpUtil
    {
        public static void DownImg(TileUri uri)
        {
            HttpWebResponse response = null;
            Stream stream = null;
            FileStream writer = null;

            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri.Url);
                request.Timeout = 60 * 1000;
                response = (HttpWebResponse)request.GetResponse();

                uri.HttpStatus = response.StatusCode;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    Console.WriteLine(response.StatusCode);
                    return;
                }

                stream = response.GetResponseStream();
                using (Image bitmap = Image.FromStream(stream))//如果不是图片这里会异常
                {
                    String ext = RecognizeImgExtName(bitmap.RawFormat);
                    String imgFileName = uri.FileNameNoExt + ext;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, bitmap.RawFormat);
                        Byte[] bytes = ms.ToArray();
                        int size = bytes.Length;//可以在这里根据图像大小进行过滤
                        writer = new FileStream(imgFileName, FileMode.OpenOrCreate, FileAccess.Write);
                        writer.Write(bytes, 0, size);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                uri.DownFailedReason = e;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    writer.Dispose();
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }

        private static string RecognizeImgExtName(ImageFormat imageFormat)
        {
            if (imageFormat.Equals(ImageFormat.Png))
            {
                return ".png";
            }
            else if (imageFormat.Equals(ImageFormat.Jpeg))
            {
                return ".jpg";
            }
            else if (imageFormat.Equals(ImageFormat.Bmp))
            {
                return ".bmp";
            }
            else if (imageFormat.Equals(ImageFormat.Gif))
            {
                return ".gif";
            }
            else if (imageFormat.Equals(ImageFormat.Icon))
            {
                return ".icon";
            }
            else if (imageFormat.Equals(ImageFormat.Exif))
            {
                return ".exif";
            }
            else if (imageFormat.Equals(ImageFormat.Tiff))
            {
                return ".tiff";
            }
            else if (imageFormat.Equals(ImageFormat.Emf))
            {
                return ".emf";
            }
            throw new Exception("不能识别的图片格式(ImageFormat)");
        }
    }
}

 

 

 

你可能感兴趣的:(C#)