• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#图片下载的标准写法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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

图片下载的标准写法
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)");
        }
    }
}

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#,反射和直接调用的效率差别发布时间:2022-07-13
下一篇:
C#爬虫框架实现概述发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap