Translate

顯示具有 Windows Form 應用程式 標籤的文章。 顯示所有文章
顯示具有 Windows Form 應用程式 標籤的文章。 顯示所有文章

2015年11月13日 星期五

url 上的 picture 壓縮

   private void button5_Click(object sender, EventArgs e)
        {
            string strtext = "https://s1.yimg.com/bt/api/res/1.2/tg4y9A._NGcShMlg5HOcUQ--/YXBwaWQ9eW5ld3NfbGVnbztmaT1pbnNldDtoPTM3MjtpbD1wbGFuZTtxPTc5O3c9NjYy/http://media.zenfs.com/zh_hant_tw/News/tvbs/TVBS-N_CLEAN_10M_20151110_13-40-03.mp4_20151110_141351.278.jpg";

            WebRequest request = WebRequest.Create(new Uri(strtext));
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.ContentLength > 0)
            {
                pictureBox1.Image = BufferToImage(CompressionImage(response.GetResponseStream(), 100));
            }
        }
        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }

        private byte[] CompressionImage(Stream fileStream, long quality)
        {
            using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
            {
                int h, w;

                if (img.Width <= 100)
                {
                    h = img.Height;
                    w = img.Width;
                }
                else
                {
                    w = 100;
                    h = 100 * img.Height / img.Width;
                }

                using (Bitmap bitmap = new Bitmap(img, w, h))//預設為長寬各為100px
                {
                    ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
                    System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
                    EncoderParameters myEncoderParameters = new EncoderParameters(1);
                    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
                    myEncoderParameters.Param[0] = myEncoderParameter;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitmap.Save(ms, CodecInfo, myEncoderParameters);
                        myEncoderParameters.Dispose();
                        myEncoderParameter.Dispose();
                        return ms.ToArray();
                    }
                }
            }
        }
        /// <summary>
        /// 將 Byte 陣列轉換為 Image。
        /// </summary>
        /// <param name="Buffer">Byte 陣列。</param>      
        private static System.Drawing.Image BufferToImage(byte[] Buffer)
        {
            if (Buffer == null || Buffer.Length == 0) { return null; }
            byte[] data = null;
            System.Drawing.Image oImage = null;
            Bitmap oBitmap = null;
            //建立副本
            data = (byte[])Buffer.Clone();
            try
            {
                MemoryStream oMemoryStream = new MemoryStream(Buffer);
                //設定資料流位置
                oMemoryStream.Position = 0;
                oImage = System.Drawing.Image.FromStream(oMemoryStream);
                //建立副本
                oBitmap = new Bitmap(oImage);
            }
            catch
            {
                throw;
            }
            //return oImage;
            return oBitmap;
        }

2013年9月9日 星期一

dataGridView 核對欄位設定全選欄位

先宣告一個控制項目gvckall,之後再取得dataGridView1裡的chk欄位控制項的位置,將位置設定好即可

使用時機
Form1()
dataGridView1_ColumnWidthChanged

程式碼參考

        void InitColumnHeader()
        {
            dataGridView1.Controls.Add(gvckall);
            int ix = (dataGridView1.RowHeadersWidth + chk.Width / 2 - gvckall.Width / 2);
            int iy = (dataGridView1.ColumnHeadersHeight / 2 - gvckall.Height / 2);
            gvckall.Location = new Point(ix, iy);
 //chk.MinimumWidth = gvckall.Width;  //最小寬度設定
        }

DataGridView第一欄調整寬度

List<string> ls = new List<string>(); //資料筆數
dataGridView1.TopLeftHeaderCell.Value = "序號";
 int iwidt = ls.Count.ToString().Length; //判定目前數字有幾位數
dataGridView1.RowHeadersWidth = 20 * iwidt + 20;

2012年10月19日 星期五

網路芳鄰的共用資料夾取檔

管理工具要連到遠端電話的共用資料夾取得檔案
並可上傳檔案
但老是卡在帳號密碼不合~_~
故目前解決的方法,先寫一個bat檔,先記遠端資料夾的帳密,等關了之後再清除
或許以後會想出更好的方法
View


帳號為abc
密碼為abc123456
net user "\\192.168.1.100" abc123456 /user:"abc"

net use \\192.168.1.100\ /delete

當離開在進行刪除→http://msdn.microsoft.com/en-us/library/system.windows.forms.application.applicationexit.aspx


static void Main()
{

      Application.ApplicationExit += new EventHandler(OnApplicationExit);
}


 //當離開程執行bat檔
static private void OnApplicationExit(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("刪除的.bat");
}

2012-10-26

後來發現好像也不用這麼麻煩了
直接在欲連線的電腦先設定好帳戶資訊就可以了
在執行→輸入 cmd→再輸入control userpasswords2
然後會開啟帳戶介面→選擇進階→新增/編輯帳戶就可以了XDD
ref→http://ithelp.ithome.com.tw/question/10040025?tag=rt.rq


2011年12月14日 星期三

ZIP靨縮(子資料夾循迴

引用:http://blog.sina.com.cn/s/blog_456cc2c401000c70.html


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
/// <summary>
/// ZipFloClass 的摘要说明
/// </summary>
public class ZipFloClass
{
    public void ZipFile(string strFile, string strZip)
    {
        if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
            strFile += Path.DirectorySeparatorChar;
        ZipOutputStream s = new ZipOutputStream(File.Create(strZip));
        s.SetLevel(6); // 0 - store only to 9 - means best compression
        zip(strFile, s, strFile);
        s.Finish();
        s.Close();
    }

    private void zip(string strFile, ZipOutputStream s, string staticFile)
    {
        if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar) strFile += Path.DirectorySeparatorChar;
        Crc32 crc = new Crc32();
        string[] filenames = Directory.GetFileSystemEntries(strFile);
        foreach (string file in filenames)
        {
            if (Directory.Exists(file))
            {
                zip(file, s, staticFile);
            }
            else // 否则直接压缩文件
            {
                //打开压缩文件
                FileStream fs = File.OpenRead(file);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                string tempfile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                ZipEntry entry = new ZipEntry(tempfile);
                entry.DateTime = DateTime.Now;
                entry.Size = fs.Length;
                fs.Close();
                crc.Reset();
                crc.Update(buffer);
                entry.Crc = crc.Value;
                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
            }
        }
    }
}

、、、、、、、、、、、、、、、
using System;
using System.Data;
using System.Web;
using System.Text;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;

/// <summary>
/// UnZipFloClass 的摘要说明
/// </summary>
public class UnZipFloClass
{
    public string unZipFile(string TargetFile, string fileDir)
    {
        string rootFile = " ";
        try
        {
            //读取压缩文件(zip文件),准备解压缩
            ZipInputStream s = new ZipInputStream(File.OpenRead(TargetFile.Trim()));
            ZipEntry theEntry;
            string path = fileDir;                  
            //解压出来的文件保存的路径
            string rootDir = " ";                      
            //根目录下的第一个子文件夹的名称
            while ((theEntry = s.GetNextEntry()) != null)
            {
                rootDir = Path.GetDirectoryName(theEntry.Name);                        
                //得到根目录下的第一级子文件夹的名称
                if (rootDir.IndexOf("\\") >= 0)
                {
                    rootDir = rootDir.Substring(0, rootDir.IndexOf("\\") + 1);
                }
                string dir = Path.GetDirectoryName(theEntry.Name);                  
                //根目录下的第一级子文件夹的下的文件夹的名称
                string fileName = Path.GetFileName(theEntry.Name);                  
                //根目录下的文件名称
                if (dir != " " )                                                      
                //创建根目录下的子文件夹,不限制级别
                {
                    if (!Directory.Exists(fileDir + "\\" + dir))
                    {
                        path = fileDir + "\\" + dir;                                              
                        //在指定的路径创建文件夹
                        Directory.CreateDirectory(path);
                    }
                }
                else if (dir == " " && fileName != "")                                            
                 //根目录下的文件
                {
                    path = fileDir;
                    rootFile = fileName;
                }
                else if (dir != " " && fileName != "")                                            
                    //根目录下的第一级子文件夹下的文件
                {
                    if (dir.IndexOf("\\") > 0)                                                          
                        //指定文件保存的路径
                    {
                        path = fileDir + "\\" + dir;
                    }
                }
                if (dir == rootDir)                                                                                
                //判断是不是需要保存在根目录下的文件
                {
                    path = fileDir + "\\" + rootDir;
                }

                //以下为解压缩zip文件的基本步骤
                //基本思路就是遍历压缩文件里的所有文件,创建一个相同的文件。
                if (fileName != String.Empty)
                {
                    FileStream streamWriter = File.Create(path + "\\" + fileName);
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            s.Close();
            return rootFile;
        }
        catch (Exception ex)
        {
            return "1; " + ex.Message;
        }
    }  
}

2011年12月2日 星期五

Visual C# 或 Microsoft Visual Basic 使用者入門教學課程

http://msdn.microsoft.com/zh-tw/library/dd492171.aspx
MSDN提供的教學課程,共有四種,適合初學者磨刀…

引用內容如下:

教學課程 1:建立圖片檢視器

建置一個從檔案載入圖片並將圖片顯示在視窗中的程式。 了解如何拖曳表單上的控制項 (例如按鈕和圖片方塊)、設定其屬性,以及使用容器來順暢地調整表單的大小。 開始撰寫程式碼。

教學課程 2:建立迷宮程式

建置一個迷宮遊戲,使用者必須移動滑鼠指標而不能碰觸到任何圍牆。 了解如何使用 Panel 容器來配置表單、使用 Label 控制項建置迷宮、撰寫程式碼來顯示訊息方塊、設定滑鼠事件的事件處理常式、讓程式播放音效,以及使用類別來組織程式碼。

教學課程 3:建立數學測驗

建置一個計時的數學測驗遊戲,玩家必須在指定的時間內回答四個隨機的算術問題。 了解如何使用 Random 類別產生隨機數字、使用 Timer 控制項觸發事件、使用 if else 陳述式控制程式流程,以及執行基本的算術運算。

教學課程 4:建立配對遊戲

建置一個配對遊戲,而遊戲玩家必須配對隱藏的圖示。 了解如何使用 List 物件來保留物件、使用 foreach 迴圈、使用參考變數追蹤表單的狀態、建置可用於多個物件的事件處理常式,以及讓計時器於啟動時剛好引發一次。


從一些基本的地方,總能收獲很多呀…XDD