我要根据这个图片链接地址用winfrom获取到它的像素宽度和高度,怎么获取呀?
代码怎么写呀
12 个解决方案
#1
我是用winform写的,一个picturebox,一个button,两个label,两个textbox。
using System;我是先下载图片,在生成实例bmp,获取bmp的宽度和高度。
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;
}
}
}
#2
private static string filepath = "c:\\pic.bmp";
这里指的是下载图片到c盘文件夹并取名为pic.bmp么???
这里指的是下载图片到c盘文件夹并取名为pic.bmp么???
#3
好吧!我加注释!
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; //初始化picturebox1控件的背景为黑色
}
//button1的按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
WebClient mywebclient = new WebClient(); //url连接新实例
mywebclient.DownloadFile(url, filepath); //下载图片到指定地址
Bitmap bmp = new Bitmap(filepath); //根据地址生成新bitmap实例bmp
int w_bmp = bmp.Width; //bmp宽度
int h_bmp = bmp.Height; //bmp高度
textBox1.Text = Convert.ToString(w_bmp); //显示宽度
textBox2.Text = Convert.ToString(h_bmp); //显示高度
pictureBox1.Image = bmp; //显示图片
}
}
}
#4
下回回复加上引用,不然没提示。
#5
这是什么情况呀???
#6
路径格式不对啊,把\改成\\试试
#7
改成\\还是不对呀
#8
/// <summary>
/// 获取图片的大小和尺寸
/// </summary>
/// <param name="aPhotoUrl">图片url</param>
/// <param name="iWidth">图片宽度</param>
/// <param name="iHeight">图片高度</param>
private void GetPhotoInfo(string aPhotoUrl, ref int iWidth, ref int iHeight)
{
try
{
Uri mUri = new Uri(aPhotoUrl);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(mUri);
mRequest.Method = "GET";
mRequest.Timeout = 200;
HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();
Stream mStream = mResponse.GetResponseStream();
Image mImage = Image.FromStream(mStream);
iWidth = mImage.Width;
iHeight = mImage.Height;
mStream.Close();
}
catch (Exception e)
{
//MessageBox.Show(aPhotoUrl + "获取失败");
}
}
#9
#10
我添加了using System.Net;这个引用了怎么还是
#11
用这个方法我怎么拿到的宽度和高度都是0呢???
#12
这是为什么呀?为什么是0呢没有拿到图片的宽度和高度呀
#1
我是用winform写的,一个picturebox,一个button,两个label,两个textbox。
using System;我是先下载图片,在生成实例bmp,获取bmp的宽度和高度。
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;
}
}
}
#2
private static string filepath = "c:\\pic.bmp";
这里指的是下载图片到c盘文件夹并取名为pic.bmp么???
这里指的是下载图片到c盘文件夹并取名为pic.bmp么???
#3
好吧!我加注释!
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; //初始化picturebox1控件的背景为黑色
}
//button1的按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
WebClient mywebclient = new WebClient(); //url连接新实例
mywebclient.DownloadFile(url, filepath); //下载图片到指定地址
Bitmap bmp = new Bitmap(filepath); //根据地址生成新bitmap实例bmp
int w_bmp = bmp.Width; //bmp宽度
int h_bmp = bmp.Height; //bmp高度
textBox1.Text = Convert.ToString(w_bmp); //显示宽度
textBox2.Text = Convert.ToString(h_bmp); //显示高度
pictureBox1.Image = bmp; //显示图片
}
}
}
#4
下回回复加上引用,不然没提示。
#5
这是什么情况呀???
#6
路径格式不对啊,把\改成\\试试
#7
改成\\还是不对呀
#8
/// <summary>
/// 获取图片的大小和尺寸
/// </summary>
/// <param name="aPhotoUrl">图片url</param>
/// <param name="iWidth">图片宽度</param>
/// <param name="iHeight">图片高度</param>
private void GetPhotoInfo(string aPhotoUrl, ref int iWidth, ref int iHeight)
{
try
{
Uri mUri = new Uri(aPhotoUrl);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(mUri);
mRequest.Method = "GET";
mRequest.Timeout = 200;
HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();
Stream mStream = mResponse.GetResponseStream();
Image mImage = Image.FromStream(mStream);
iWidth = mImage.Width;
iHeight = mImage.Height;
mStream.Close();
}
catch (Exception e)
{
//MessageBox.Show(aPhotoUrl + "获取失败");
}
}
#9
#10
我添加了using System.Net;这个引用了怎么还是
#11
用这个方法我怎么拿到的宽度和高度都是0呢???
#12
这是为什么呀?为什么是0呢没有拿到图片的宽度和高度呀