C#一些小知識點

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   sp   for   

1. 在Load時候由代碼來做控制項PictureBox,並且用代碼將圖片載入進去:

 private void Form2_Load(object sender, EventArgs e)        {            DirectoryInfo dir = new DirectoryInfo(@"F:\image\car");            FileInfo[] files = dir.GetFiles("*.jpg");            foreach (FileInfo f in files)            {                //造PictureBox                PictureBox pb = new PictureBox();                //載入圖片                pb.Image = Image.FromFile(f.FullName);                //設定圖片框大小                pb.Size = new System.Drawing.Size(100, 80);                //設定縮放                pb.SizeMode = PictureBoxSizeMode.Zoom;                //加到流式布局控制項中去                flowLayoutPanel1.Controls.Add(pb);                //給圖片框加上click事件                pb.Click +=pb_Click;            }        }

2. 關於事件,sender是指事件來源,比如在PictureBox上加的點擊事件,則事件來源就是該PictureBox,sender是個object類型,所以要強轉一下。E是事件數目據,EventArgs是事件數目據的基類。

 private void pb_Click(object sender, EventArgs e)        {            picShow.Image = ((PictureBox)sender).Image;        }

3. 代碼加事件,去事件

 pb.Click +=pb_Click;  去事件用”-=”,加完事件之後Tab鍵產生。

4.   #region 這是自訂的工具函數

     #region 這是自訂的工具函數     中間一般將自己寫的工具函數(供其他函數調用的)包在中間,這樣能使代碼更清晰。     #endregion

5. 關閉程式

Application.Exit();是整個應用程式結束,比this.close()更直接。This.close();是關閉視窗導致程式結束。

6. 在一個圖片上面的指定位置寫文字:

            //載入映像            Image img = Image.FromFile(@"E:\cyl.jpg");            Graphics g = Graphics.FromImage(img);            //寫字            string s = "http://www.ITNBA.COM";            Font font = new System.Drawing.Font("隸書", 14, FontStyle.Italic);            SolidBrush brush = new SolidBrush(Color.Blue);            g.DrawString(s, font, brush, 10, 10);                        //儲存            img.Save(@"E:\cyl0.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

7. 測定一個字串的寬和高:

           //測定字型的寬與高            SizeF size = g.MeasureString(s,font);            size.Width是測量出的字串的寬度            ssize.Height是測量出的字串的高度

8. 在一個圖片上畫上另一個圖片(加浮水印)。

           //載入映像            Image img = Image.FromFile(@"E:\cyl.jpg");//靶心圖表像            Image wm = Image.FromFile(@"E:\bbb.gif");//要加的浮水印            //取得畫板            Graphics g = Graphics.FromImage(img);            //畫映像            g.DrawImage(wm, 10, 10);            //儲存圖片            img.Save(@"E:\cyl2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

9. 將圖片放進資料庫   

            //取出文字框中代號            string code = textBox1.Text;            //將圖片變為位元組數組讀入記憶體            FileStream stream = new FileStream(fileName, FileMode.Open);            byte[] picture = new byte[stream.Length];            stream.Read(picture, 0, picture.Length);            stream.Close();            //將圖片的位元組數組放進資料庫儲存            PictureTableAdapter adapter = new PictureTableAdapter();            adapter.AddPicture(code, picture);

10. 將圖片從資料庫取出

            //從資料庫取圖片            MyDB.PictureDataTable table = new PictureTableAdapter().GetDataByCode(textBox1.Text);            if (table.Rows.Count > 0)            {                //用數組接收                byte[] b = table[0].Pic;                //將圖片寫入硬碟                FileStream stream = new FileStream(@"D:\test.jpg", FileMode.Create);                stream.Write(b, 0, b.Length);                stream.Close();

 

C#一些小知識點

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.