日期:2014-05-17  浏览次数:20882 次

C#根据图片链接地址获取它的像素宽度和高度
http://ww2.sinaimg.cn/bmiddle/6128e4c9jw1e8w8ckn6taj20np0hs0uw.jpg
我要根据这个图片链接地址用winfrom获取到它的像素宽度和高度,怎么获取呀?
代码怎么写呀
C# winfrom 图片像素

------解决方案--------------------
我是用winform写的,一个picturebox,一个button,两个label,两个textbox。
using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //private static string url = "http://www3.clustrmaps.com/stats/maps-layer/958000/958172/www.cnblogs.com-sun8134--thumb-dots.png";
        private static string url = "http://ww2.sinaimg.cn/bmiddle/6128e4c9jw1e8w8ckn6taj20np0hs0uw.jpg";
        private static string filepath = "c:\\pic.bmp";
        public Form1()
        {
            InitializeComponent();
            pictureBox1.BackColor = Color.Black;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            WebClient mywebclient = new WebClient();
            mywebclient.DownloadFile(url, filepath);
            Bitmap bmp = new Bitmap(filepath);
            int w_bmp = bmp.Width;
            int h_bmp = bmp.Height;
            textBox1.Text = Convert.ToString(w_bmp);
            textBox2.Text = Convert.ToString(h_bmp);
            pictureBox1.Image = bmp;
        }
    }
}
我是先下载图片,在生成实例bmp,获取bmp的宽度和高度。
------解决方案--------------------
好吧!我加注释!

using System;
using System.Drawing;
using System.Net;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //private static string&nb