class MyBitmap
{
private int width//位图的宽
public int Width
{
get { return width}
set { width = value}
}
private int height//位图的高
public int Height
{
get { return height}
set { height = value}
}
private int postColor//颜色深度
public int PostColor
{
get { return postColor}
set { postColor = value}
}
private Bitmap mBitmap//位图内置对象,引用系统内置Bitmap
public Bitmap MBitmap
{
get { return mBitmap}
set { mBitmap = value}
}
/// <summary>
/// 构造方法,构造一个Bitmap类
/// </summary>
/// <param name="fileurl">文件路径,包含文件名.</param>
public MyBitmap(string fileurl)
{
FileStream fs = new FileStream(fileurl, FileMode.Open)//引用文件 *** 作流
fs.Seek(18, 0)//将流的初始值设为19,即跳过18位.
int wbmp1 = fs.ReadByte()//读取宽的第一位
int wbmp2 = fs.ReadByte()//读取宽的第二位
int wbmp3 = fs.ReadByte()//读取宽的第三位
fs.ReadByte()//第四位在这里用不到.
int hbmp1 = fs.ReadByte()//读取高的第一位
int hbmp2 = fs.ReadByte()//读取高的第二位
int hbmp3 = fs.ReadByte()//读取高的第三位,第四位依然用不到.
Width = wbmp1 | wbmp2 <<8 | wbmp3 <<16//位移运算.得到三个位组成的一个int类型的变量,即位图的宽.
Height = hbmp1 | hbmp2 <<8 | hbmp3 <<16//位移运算.得到三个位组成的一个int类型的变量,即位图的高.
//取得颜色深度,即为多少位的图.在文件头的29位.
fs.Seek(28, 0)//跳过28位.
postColor = fs.ReadByte()//读取第29位,即颜色深度.
MBitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb)//实例化Bitmap类
fs.Seek(54, 0)//跳过文件头,即从第55位开始.
for (int Ycount = 0Ycount <MBitmap.HeightYcount++)//双层循环,遍历bmp数组,分别调用SetPixel方法
{
for (int Xcount = 0Xcount <MBitmap.WidthXcount++)
{
int a = fs.ReadByte()
int b = fs.ReadByte()
int c = fs.ReadByte()
int color = a | b <<8 | c <<16
//因为行序是颠倒的,所以要让它倒回来,需要用行高减去它当前的行数再减一,防止越界.
MBitmap.SetPixel(Xcount, MBitmap.Height - Ycount - 1, Color.FromArgb(color))
}
}
fs.Close()//关闭文件流
}
/// <summary>
/// 绘制位图
/// </summary>
/// <param name="g">Graphics对象g</param>
public void Draw(Graphics g,Bitmap bmp)
{
g.DrawImage(bmp, 0, 0)//绘制bmp图
}
public Bitmap SmallBitmap(Bitmap b)
{
int w = b.Width / 2
int h = b.Height / 2
Bitmap bmp = new Bitmap(b, w, h)
for (int Ycount = 0Ycount <hYcount++)
{
for (int Xcount = 0Xcount <wXcount++)
{
Color c1 = b.GetPixel(Xcount * 2, Ycount * 2)
Color c2 = b.GetPixel(Xcount * 2, Ycount * 2 + 1)
Color c3 = b.GetPixel(Xcount * 2 + 1, Ycount * 2)
Color c4 = b.GetPixel(Xcount * 2 + 1, Ycount * 2 + 1)
int c1r = (16711680 &c1.ToArgb()) >>16
int c1g = (65280 &c1.ToArgb()) >>8
int c1b = (255 &c1.ToArgb())
int c2r = (16711680 &c2.ToArgb()) >>16
int c2g = (65280 &c2.ToArgb()) >>8
int c2b = (255 &c2.ToArgb())
int c3r = (16711680 &c3.ToArgb()) >>16
int c3g = (65280 &c3.ToArgb()) >>8
int c3b = (255 &c3.ToArgb())
int c4r = (16711680 &c4.ToArgb()) >>16
int c4g = (65280 &c4.ToArgb()) >>8
int c4b = (255 &c4.ToArgb())
int cr = (c1r + c2r + c3r + c4r) / 4
int cg = (c1g + c2g + c3g + c4g) / 4
int cb = (c1b + c2b + c3b + c4b) / 4
bmp.SetPixel(Xcount, Ycount, Color.FromArgb((cr == 255 &&cg == 0 &&cb == 255) ? 0 : 255, cr, cg, cb))
}
}
return bmp
}
}
这里有一段我写的对bmp类型图片的绘制,你可以看下
如果是用sql语句插入数据的话如下:
insert
into
table(a,b,c)
value(1,2,3)
--若b需要使用默认值,如下即可:
insert
into
table(a,c)
value(1,3)
insert
into
table
value(1,2,3)
insert
into
table
select
a,b,c
from
table1
insert
into
a
select
*
from
b
从根本的上说,都是insert用法。没有利避,只看需要。
你仔细分析一下,就会发现,还是标准的insert
insert
into
表名
[表列]value[对应值]
要是变化就在前面的表列和对应值上,怎么用表列,可省略,可以动态的取得。
对应值的获取方法就更多了,可以动态的输入,比如一些存储过程。还可以从别的表中取得,也可以固定时,还可以用一些函数,等等
读取... 读取长二进制为图片..string sql = "select photo from studentinfo where studentid = " + this.Tag.ToString()
OleDbCommand cmd = new OleDbCommand(sql, connection1)
if (Convert.DBNull != cmd.ExecuteScalar())
pictureBox1.Image = Image.FromStream(new MemoryStream((Byte[])cmd.ExecuteScalar()))
放大就不知道了
保存:
string filename= textBox1.Text//"c:\\IMG_0117.jpg"
BinaryReader reader=null
FileStream myfilestream = new FileStream(filename,FileMode.Open)
try
{
reader=new BinaryReader(myfilestream)
byte[] image = reader.ReadBytes((int)myfilestream.Length)
using (SqlConnection conn = new SqlConnection("server=test05database=esdb2uid=datatranpwd=qyrl"))
{
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText =@"INSERT INTO photo(photo) VALUES (@photo)"
command.Parameters.Add("@photo", image)
conn.Open()
command.ExecuteNonQuery()
conn.Close()
MessageBox.Show("文件保存成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
}
}
}
catch(IOException ee)
{
MessageBox.Show(ee.Message.ToString())
}
finally
{
if(reader!=null)
reader.Close()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)