1. 程式人生 > >window介面下載展示網路圖片(C#)

window介面下載展示網路圖片(C#)

在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

    }
}