13
2015
07

C# 判断png图片某区域是否透明

 

    先把图片分割成无数小块,然后再判断某一块是否透明。

 

       public bool IsTransparentPalette( Bitmap bmp)
       {    
           for (int i = 0; i < bmp.Width; i++)
           {
               for (int j = 0; j < bmp.Height; j++)
               {
                   Color cl = bmp.GetPixel(i, j);
                   if (cl.A != 0)
                   {
                       return false;
                   }
               }
           }
           return true;
       }

01
2015
07

Aspose.Total for .NET 2015 - Unlimited License 注册码

 

 

网上看到的,还没验证过呢、

 

 

How to license Aspose.Total for .NET products
Add
"License.cs" [C#] OR
"License.vb" [Visual Basic] to your application
License.cs
Code:
using System;
using System.IO;
namespace LicenseHelper
{   
    public static class License
    {       
        public const string Key =
            "PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
            "UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
            "9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
            "HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
            "dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
            "wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
            "A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
            "m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
            "dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
            "0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
            "djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
            "jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
            "PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
            "3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
            "FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
            "zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
            "aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
            "Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
            "V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";
        public static Stream LStream = (Stream)new MemoryStream(Convert.FromBase64String(LicenseHelper.License.Key));
               
    }
}
License.vb
Code:
Imports System
Imports System.IO
Namespace LicenseHelper
    Public Class License
        Public Const Key As String =
            "PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
            "UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
            "9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
            "HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
            "dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
            "wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
            "A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
            "m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
            "dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
            "0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
            "djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
            "jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
            "PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
            "3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
            "FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
            "zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
            "aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
            "Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
            "V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+"
        Public Shared LStream As Stream = New MemoryStream(Convert.FromBase64String(LicenseHelper.License.Key))
    End Class
End Namespace

17
2015
03

C#List类型复制,List的深度拷贝

 

 

命名空间

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

 

 


public object Clone(object ldc)
        {
            BinaryFormatter Formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            Formatter.Serialize(stream, ldc);
            stream.Position = 0;
            object clonedObj = Formatter.Deserialize(stream);
            stream.Close();
            return clonedObj;
        }

26
2015
02

C# DataGridView 通过程序对列进行排序

      void SortDatagridview()
        {
            dgvFlux.Columns["State"].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
            dgvFlux.Sort(dgvFlux.Columns["State"], ListSortDirection.Ascending);
        }

26
2015
02

c# winform 鼠标拖动改变控件位置和鼠标拖拽改变空间大小,调用即可



    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Windows.Forms;  
      
    namespace AllTest  
    {  
        class BarcodeControl  
        {  
            #region private  
      
            private const int MIN_SIZE = 10; //对控件缩放的最小值   
            private const int BOX_SIZE = 7;  //调整大小触模柄方框大小   
      
            public bool _IsCtrlKey = false;  
            private TextBox _textbox;  
            private Control _MControl = null;  
            private bool _IsMouseDown = false;  
            private Point _oPointClicked;  
            private Color BOX_COLOR = Color.White;  
            private Label[] _lbl = new Label[8];  
            private int _startl, _startt, _startw, _starth;  
            private bool _dragging;  
            private Cursor[] _arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,   
                                                        Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,   
                                                        Cursors.SizeNESW, Cursors.SizeWE};  
     
            #endregion  
     
            #region 构造函数  
      
            /// <summary>   
            /// 构造控件拖动对象   
            /// </summary>   
            /// <param name="moveControl">需要拖动的控件 </param>   
            public BarcodeControl(Control moveControl)  
            {  
                //   
                // TODO: 在此处添加构造函数逻辑   
                //   
                _MControl = moveControl;  
                _MControl.MouseDown += new MouseEventHandler(this.Control_MouseDown);  
                _MControl.MouseUp += new MouseEventHandler(this.Control_MouseUp);  
                _MControl.MouseMove += new MouseEventHandler(this.Control_MouseMove);  
                _MControl.Click += new System.EventHandler(this.Control_Click);  
      
                //构造8个调整大小触模柄   
                for (int i = 0; i < 8; i++)  
                {  
                    _lbl[i] = new Label();  
                    _lbl[i].TabIndex = i;  
                    _lbl[i].FlatStyle = 0;  
                    _lbl[i].BorderStyle = BorderStyle.FixedSingle;  
                    _lbl[i].BackColor = BOX_COLOR;  
                    _lbl[i].Cursor = _arrArrow[i];  
                    _lbl[i].Text = "";  
                    _lbl[i].BringToFront();  
                    _lbl[i].MouseDown += new MouseEventHandler(this.handle_MouseDown);  
                    _lbl[i].MouseMove += new MouseEventHandler(this.handle_MouseMove);  
                    _lbl[i].MouseUp += new MouseEventHandler(this.handle_MouseUp);  
                }  
      
                CreateTextBox();  
                Create();  
      
                //Control_Click((object)sender, (System.EventArgs)e);   
            }  
     
            #endregion  
     
            #region 需拖动控件键盘事件  
      
            private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)  
            {  
                if (e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 39 || e.KeyValue == 40)  
                {  
                    if (e.KeyValue == 37)  
                        _MControl.Left -= 1;  
                    if (e.KeyValue == 38)  
                        _MControl.Top -= 1;  
                    if (e.KeyValue == 39)  
                        _MControl.Left += 1;  
                    if (e.KeyValue == 40)  
                        _MControl.Top += 1;  
                    MoveHandles();  
                    ControlLocality();  
                    _MControl.Visible = true;  
                }  
      
                if (e.KeyValue == 46)  
                {  
                    for (int i = 0; i < 8; i++)  
                    {  
                        _MControl.Parent.Controls.Remove(_lbl[i]);  
                    }  
                    _MControl.Parent.Controls.Remove(_MControl);  
                    _textbox.Parent.Controls.Remove(_textbox);  
                }  
      
                if (e.KeyValue == 17)  
                {  
                    _IsCtrlKey = true;  
                    //MessageBox.Show("a");   
                }  
            }  
     
            #endregion  
     
            #region 需拖动控件鼠标事件  
      
            private void Control_Click(object sender, System.EventArgs e)  
            {  
                _textbox.Focus();  
                _MControl = (sender as Control);  
                MoveHandles();  
      
                if (_IsCtrlKey == false)  
                {  
                    for (int i = 0; i < _MControl.Parent.Controls.Count; i++)  
                    {  
                        if (_MControl.Parent.Controls[i].Text.Trim().Length == 0 && _MControl.Parent.Controls[i] is Label)  
                        {  
                            _MControl.Parent.Controls[i].Visible = false;  
                        }  
                    }  
                }  
            }  
            private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)  
            {  
                _IsMouseDown = true;  
                _oPointClicked = new Point(e.X, e.Y);  
                for (int i = 0; i < 8; i++)  
                {  
                    _MControl.Parent.Controls.Add(_lbl[i]);  
                    _lbl[i].BringToFront();  
                }  
                HideHandles();  
            }  
      
            private void Control_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)  
            {  
                _IsMouseDown = false;  
                MoveHandles();  
                ShowHandles();  
                _MControl.Visible = true;  
            }  
      
            private void Control_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)  
            {  
                if (_IsMouseDown)  
                {  
                    int l = _MControl.Left + (e.X - _oPointClicked.X);  
                    int t = _MControl.Top + (e.Y - _oPointClicked.Y);  
                    int w = _MControl.Width;  
                    int h = _MControl.Height;  
                    l = (l < 0) ? 0 : ((l + w > _MControl.Parent.ClientRectangle.Width) ?  
                        _MControl.Parent.ClientRectangle.Width - w : l);  
                    t = (t < 0) ? 0 : ((t + h > _MControl.Parent.ClientRectangle.Height) ?  
                        _MControl.Parent.ClientRectangle.Height - h : t);  
                    _MControl.Left = l;  
                    _MControl.Top = t;  
                    ControlLocality();  
                }  
                //_MControl.Cursor=Cursors.SizeAll;   
            }  
     
            #endregion  
            #region 调整大小触模柄鼠标事件  
      
            private void handle_MouseDown(object sender, MouseEventArgs e)  
            {  
                _dragging = true;  
                _startl = _MControl.Left;  
                _startt = _MControl.Top;  
                _startw = _MControl.Width;  
                _starth = _MControl.Height;  
                HideHandles();  
            }  
            // 通过触模柄调整控件大小   
            //    0  1  2   
            //  7      3   
            //  6  5  4   
            private void handle_MouseMove(object sender, MouseEventArgs e)  
            {  
                int l = _MControl.Left;  
                int w = _MControl.Width;  
                int t = _MControl.Top;  
                int h = _MControl.Height;  
                if (_dragging)  
                {  
                    switch (((Label)sender).TabIndex)  
                    {  
                        //l算法:控件左边X坐标 + 鼠标在触模柄X坐标 < 控件左边X坐标 + 父控件宽度 - 控件大小 ?控件左边X坐标 + 鼠标在触模柄X坐标 :控件左边X坐标 + 父控件宽度 - 控件大小    
                        //t算法:   
                        //w算法:   
                        //h算法:   
                        case 0: // _dragging top-left sizing box   
                            l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;  
                            t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;  
                            w = _startl + _startw - _MControl.Left;  
                            h = _startt + _starth - _MControl.Top;  
                            break;  
                        case 1: // _dragging top-center sizing box   
                            t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;  
                            h = _startt + _starth - _MControl.Top;  
                            break;  
                        case 2: // _dragging top-right sizing box   
                            w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;  
                            t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;  
                            h = _startt + _starth - _MControl.Top;  
                            break;  
                        case 3: // _dragging right-middle sizing box   
                            w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;  
                            break;  
                        case 4: // _dragging right-bottom sizing box   
                            w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;  
                            h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;  
                            break;  
                        case 5: // _dragging center-bottom sizing box   
                            h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;  
                            break;  
                        case 6: // _dragging left-bottom sizing box   
                            l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;  
                            w = _startl + _startw - _MControl.Left;  
                            h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;  
                            break;  
                        case 7: // _dragging left-middle sizing box   
                            l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;  
                            w = _startl + _startw - _MControl.Left;  
                            break;  
                    }  
                    l = (l < 0) ? 0 : l;  
                    t = (t < 0) ? 0 : t;  
                    _MControl.SetBounds(l, t, w, h);  
                }  
            }  
      
            private void handle_MouseUp(object sender, MouseEventArgs e)  
            {  
                _dragging = false;  
                MoveHandles();  
                ShowHandles();  
            }  
     
            #endregion  
     
            #region private方法  
      
            private void Create()  
            {  
                //_IsMouseDown = true;   
                //_oPointClicked = new Point(e.X,e.Y);   
                for (int i = 0; i < 8; i++)  
                {  
                    _MControl.Parent.Controls.Add(_lbl[i]);  
                    _lbl[i].BringToFront();  
                }  
                HideHandles();  
            }  
      
            private void CreateTextBox()  
            {  
                _textbox = new TextBox();  
                _textbox.CreateControl();  
                _textbox.Parent = _MControl.Parent;  
                _textbox.Width = 0;  
                _textbox.Height = 0;  
                _textbox.TabStop = true;  
                _textbox.KeyDown += new System.Windows.Forms.KeyEventHandler(textBox_KeyDown);  
            }  
      
            private void ControlLocality()  
            {  
                if (_MControl.Location.X < 0)  
                {  
                    _MControl.Visible = false;  
                    _MControl.Left = 0;  
                }  
                if (_MControl.Location.Y < 0)  
                {  
                    _MControl.Visible = false;  
                    _MControl.Top = 0;  
                }  
                if (_MControl.Location.X + _MControl.Width > _MControl.Parent.Width)  
                {  
                    _MControl.Visible = false;  
                    _MControl.Left = _MControl.Parent.Width - _MControl.Width;  
                }  
                if (_MControl.Location.Y + _MControl.Height > _MControl.Parent.Height)  
                {  
                    _MControl.Visible = false;  
                    _MControl.Top = _MControl.Parent.Height - _MControl.Height;  
                }  
            }  
      
            private void HideHandles()  
            {  
                for (int i = 0; i < 8; i++)  
                {  
                    _lbl[i].Visible = false;  
                }  
            }  
      
            private void MoveHandles()  
            {  
                int sX = _MControl.Left - BOX_SIZE;  
                int sY = _MControl.Top - BOX_SIZE;  
                int sW = _MControl.Width + BOX_SIZE;  
                int sH = _MControl.Height + BOX_SIZE;  
                int hB = BOX_SIZE / 2;  
                int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,   
                                              sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};  
                int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,   
                                              sY + sH-hB, sY + sH-hB, sY + sH / 2};  
                for (int i = 0; i < 8; i++)  
                {  
                    _lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);  
                }  
            }  
      
            private void ShowHandles()  
            {  
                if (_MControl != null)  
                {  
                    for (int i = 0; i < 8; i++)  
                    {  
                        _lbl[i].Visible = true;  
                    }  
                }  
            }  
     
            #endregion   
        }  
    }  

05
2015
02

C#操作Ftp上传,下载,删除文件,创建目录,删除目录,获得文件列表

 


using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace ConvertData
{
    class FtpUpDown
    {
        string ftpServerIP;
        string ftpUserID;
        string ftpPassword;
        FtpWebRequest reqFTP;
        private void Connect(String path)//连接ftp
        {
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
            // 指定数据传输类型
            reqFTP.UseBinary = true;
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        }

21
2015
01

C#循环中使用Random生成随机数有重复问题

 


如果在高并发的情况下,Random类所取到的系统时钟种子接近甚至完全一样,就很有可能出现重复,这里用循环来举例

for (int i = 0; i < 10; i++)
{
    Random rd = new Random();  //无参即为使用系统时钟为种子
    Console.WriteLine(rd.Next().ToString());
}

21
2014
06

无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。

 

在加载 Silverlight的项目过程中,提示错误 :

 

无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。

 

 

这多半是由于关联dll的问题照成的, 可能是反射的dll代码还依赖于其他的dll, 但是并没有提供, 或者dll 有更新,但是没有提供最新的dll引起的, 好好检查一下dll文件一般都能解决。

07
2014
05

C#类型后加问号如decimal?

 


decimal?是可空类型就是可以将值设置为Null,decimal 不能设置为null

decimal a = 100; //对
decimal a = null; //错
decimal? a = 100; //对
decimal? a = null; //对

decimal? a = null;
Console.WriteLine(a.HasValue); //输出False

27
2014
03

C#中backgroundWorker显示执行进度,和窗体UI控件交互显示进度信息

 

需要设置backgroundWorker的两个属性:
this.backgroundWorker.WorkerReportsProgress = true;

 this.backgroundWorker.WorkerSupportsCancellation = true;  这个属性支持辅助线程的取消。

 

   private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.backgroundWorker.ReportProgress(100, "系统正在根据您的查询条件取数据");
            DoSome();//取数据源动作           
            if (this.dt.Rows.Count == 0)//数据源为空
            {
                this.backgroundWorker.CancelAsync();
            }
          
            for (int j = 0; j <= this.dgPDF.RowCount - 1; j++)
             {
                    this.dgPDF["Pages", j].Value = "";
                    convert(this.dgPDF["Input", j].Value.ToString(), this.dgPDF["Output", j].Value.ToString());
                    this.backgroundWorker2.ReportProgress(j, string.Format("{0}/{1}转换中...", j + 1, this.dgPDF.RowCount));   
             }
            this.backgroundWorker.ReportProgress(100, "Excel导入已经完成");   
        }