C#语言 获取网络图片分辨率的方法
时间: 2023-08-10 08:22:22 浏览: 74
你可以使用System.Drawing命名空间中的Image类来获取网络图片的分辨率。以下是示例代码:
```
using System.Drawing;
using System.Net;
// 获取网络图片分辨率的方法
public static Size GetImageSize(string imageUrl)
{
Size size = new Size();
WebRequest webRequest = WebRequest.Create(imageUrl);
using (WebResponse webResponse = webRequest.GetResponse())
{
using (Stream stream = webResponse.GetResponseStream())
{
using (Image image = Image.FromStream(stream))
{
size.Width = image.Width;
size.Height = image.Height;
}
}
}
return size;
}
```
你可以将网络图片的URL作为参数传递给GetImageSize方法,它将返回一个Size对象,其中包含了该图片的宽度和高度。
相关问题
C#语言 获取图片分辨率的方法
在 C# 中获取图片分辨率,可以使用 System.Drawing 命名空间提供的 Image 类的属性。下面是一个获取分辨率的示例代码:
```csharp
using System.Drawing;
// 读取图片
Image image = Image.FromFile("image.jpg");
// 获取分辨率
int width = image.Width;
int height = image.Height;
// 输出分辨率
Console.WriteLine($"图片分辨率为:{width}x{height}");
// 释放资源
image.Dispose();
```
在上面的示例中,首先使用 `Image.FromFile` 方法读取图片,然后通过 `image.Width` 和 `image.Height` 属性获取图片的宽度和高度,最后输出分辨率。为了释放资源,需要调用 `image.Dispose()` 方法。
C#获取当前显示器分辨率
您好,获取当前显示器分辨率可以使用System.Windows.Forms.Screen类。该类提供了CurrentScreen属性,可以获取当前屏幕的分辨率。
下面是获取当前屏幕分辨率的示例代码:
```
using System.Windows.Forms;
...
// 获取当前屏幕分辨率
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
// 输出屏幕分辨率
Console.WriteLine("Screen Resolution: {0} x {1}", screenWidth, screenHeight);
```
上述代码中,使用Screen.PrimaryScreen获取当前屏幕,然后使用Bounds属性获取屏幕的大小,最后得到当前屏幕的分辨率。
希望能够帮助到您。如有疑问,请随时提出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)