C# 一些基本的控件代码
c# 怎么用代码设置picturebox的大小,位置
picturebox.Location = new Point(x,y) 位置picturebox.Size = new Size(0,0);大小,Size里面的参数自己设置,前面是宽度,后面是高度
如何用C# 打开和保存BMP文件
操作如下:
Bitmap _Bitmap = (Bitmap)Image.FromFile(@"c:\1.BMP");
BitmapData _BitmapData = _Bitmap.LockBits(new Rectangle(0, 0, _Bitmap.Width, _Bitmap.Height), ImageLockMode.ReadWrite, _Bitmap.PixelFormat);
byte[] _Value = new byte[_BitmapData.Stride * _BitmapData.Height];
Marshal.Copy(_BitmapData.Scan0, _Value, 0, _Value.Length);
写回去还是用 Marshal.Copy.
C#--整型与字节数组byte[]之间的转换
int i = 123;
byte [] intBuff = BitConverter.GetBytes(i); // 将 int 转换成字节数组
lob.Write(intBuff, 0, 4);
i = BitConverter.ToInt32(intBuff, 0); // 从字节数组转换成 int
double x = 123.456;
byte [] doubleBuff = BitConverter.GetBytes(x); // 将 double 转换成字节数组
lob.Write(doubleBuff, 0, 8);
x = BitConverter.ToDouble(doubleBuff, 0); // 从字节数组转换成 double
C#获取鼠标相对当前窗口坐标的实现方法
使用PointToClient计算鼠标相对于某个控件的坐标,如下
Point screenPoint = Control.MousePosition;//鼠标相对于屏幕左上角的坐标
Point formPoint = this.PointToClient(Control.MousePosition);//鼠标相对于窗体左上角的坐标
Point contextMenuPoint = contextMenuStrip1.PointToClient(Control.MousePosition); //鼠标相对于contextMenuStrip1左上角的坐标


