在windows应用程序中,显示网络图片地址对应的图像
示例:
String picUrl = "http://t2.hddhhn.com/uploads/tu/201707/115/56.jpg";
QRTool.HelpForm.Show(picUrl); // 展示图像
下载展示逻辑:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace QRTool
{
/// <summary>
/// QRTool.Ad.SetLinkLableAd(panel1.Controls); // 添加链接
/// </summary>
public class Ad
{
/// <summary>
/// 工具辅助展示, SetToolAd(panel1.Controls);
/// </summary>
public static void SetLinkLableAd(Control.ControlCollection form, int x = 249, int y = 259)
{
String picTitle = DependentFiles.GetWebConfig("picTitle");
//picTitle = "功能测试";
if (!picTitle.Equals(""))
{
System.Windows.Forms.LinkLabel label = new System.Windows.Forms.LinkLabel();
form.Add(label);
//
// label
//
label.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
label.AutoSize = true;
label.LinkColor = System.Drawing.Color.Red;
label.Location = new System.Drawing.Point(x, y);
label.Name = "linkLabelToolAd";
label.Size = new System.Drawing.Size(83, 12);
label.TabStop = true;
//label.Text = "二维码Hepform";
label.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(linkLabel2_LinkClicked);
label.Text = picTitle; // 标签名称
label.Visible = true;
}
}
/// <summary>
/// 显示图像信息
/// </summary>
private static void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
String picUrl = DependentFiles.GetWebConfig("picUrl"); // 获取配置信息
if (!picUrl.Equals("")) QRTool.HelpForm.Show(picUrl); // 展示图像
//String picUrl = "https://git.oschina.net/joymeng/channelDemo/raw/master/demo_自测用例/0000842_小米/v4.8.30/测试 - 支付异常(创建订单失败).png";
//picUrl = "http://avatar.csdn.net/0/F/1/1_ljinshuan.jpg";
//QRTool.HelpForm.Show(picUrl);
}
}
public class HelpForm : Form
{
#region 图像信息展示接口
/// <summary>
/// 在HelpForm显示image图像
/// </summary>
public static void Show(Bitmap image)
{
new HelpForm(image).Show();
}
/// <summary>
/// 在HelpForm显示picUrl对应的图像 如:picUrl = "http://avatar.csdn.net/0/F/1/1_ljinshuan.jpg"
/// </summary>
public static void Show(String picUrl)
{
new HelpForm(picUrl).Show();
}
#endregion
#region HelpForm相关逻辑
/// <summary>
/// 设置在界面展示的图像
/// </summary>
private void Init(Bitmap image)
{
this.SuspendLayout();
//
// HelpForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.ClientSize = new System.Drawing.Size(889, 550);
this.Name = "HelpForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "";
this.SizeChanged += new System.EventHandler(this.HelpForm_SizeChanged);
this.ResumeLayout(false);
this.ShowInTaskbar = false;
this.BackgroundImage = image;
this.Width = image.Width + this.Width - this.ClientRectangle.Width + 10;
this.Height = image.Height + this.Height - this.ClientRectangle.Height + 10;
this.BackgroundImageLayout = ImageLayout.Center;
isload = true;
}
bool isload = false;
public HelpForm(Bitmap image)
{
Init(image); // 设置显示图像
}
public HelpForm(String picUrl)
{
Bitmap image = getWebImage(picUrl); // 获取图像
Init(image);
}
private void HelpForm_SizeChanged(object sender, EventArgs e)
{
if (isload)
{
this.BackgroundImageLayout = ImageLayout.Zoom;
}
}
/// <summary>
/// 获取picUrl的图像
/// </summary>
private Bitmap getWebImage(String picUrl)
{
WebClient client = new WebClient();
byte[] data = client.DownloadData(picUrl); // 下载url对应图像数据
Bitmap image = null;
if (data.Length > 0) image = BytesToBitmap(data);// 转化为图像
return image;
}
/// <summary>
/// byte[] 转换 Bitmap
/// </summary>
public static Bitmap BytesToBitmap(byte[] Bytes)
{
MemoryStream stream = null;
try
{
stream = new MemoryStream(Bytes);
return new Bitmap((Image)new Bitmap(stream));
}
catch (Exception ex)
{ }
stream.Close();
return null;
}
/// <summary>
/// Bitmap转byte[]
/// </summary>
public static byte[] Bitmap2Byte(Bitmap bitmap)
{
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Jpeg);
byte[] data = new byte[stream.Length];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(data, 0, Convert.ToInt32(stream.Length));
return data;
}
}
#endregion
}
}