当前位置:首页 » 操作系统 » c读取数据库中图片

c读取数据库中图片

发布时间: 2025-02-28 09:52:50

A. 数据库以img存储,如何读取图片

直接使用企业管理器好像没有办法操作吧,通过软件或自己做个小软件读取。

#region //读取数据库中图片到内存.并显示
public void LoadToMemoryAndDisable(string serverAdress, string database)
{
//读取数据库中图片到内存.并显示
sqlConnection conn = new SqlConnection("server=" + serverAdress + ";integrated security = sspi;database = " + database);
SqlCommand cmd = new SqlCommand("select * from imgtable where imgname like '%bmp%'", conn);
conn.Open();
SqlDataReader dr;
try
{
dr = cmd.ExecuteReader();
dr.Read();
System.Data.SqlTypes.SqlBinary sb = dr.GetSqlBinary(2);
//或byte[] imageData = (byte[])dr[2];
MemoryStream ms = new MemoryStream(sb.Value);//在内存中操作图片数据
Bitmap bmp = new Bitmap(Bitmap.FromStream(ms));
this.pictureBox1.Image = bmp;
dr.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
#endregion

B. C#中从数据库下载文件

首先要把数据库里面对应的那个字段读出来 然后进行处理
下面的例子是我从数据库读取图片的
OracleConnection conn = new OracleConnection(connstring);
conn.Open();
label1.Text = "开始时间:" + DateTime.Now;
string begin = "数据库连接正常,开始时间" + DateTime.Now;
SetInfo("", begin);
string SQL = "select ID from bs_buylicense_attach t where t.file_size>204800 order by t.file_size asc"; //rownum<100 and
string ConnString = SQL;
OracleDataAdapter sda = new OracleDataAdapter(ConnString, conn);
DataTable table = new DataTable();
sda.Fill(table);
List<object> lst = ModelConvertHelper.DtCovertIList(table);
foreach (object o in lst)
{
string path = "C:\\1.png"; ;
string lst_ID = o.ToString(); //查询返回来对应的数据的ID
string filter = "select * from bs_buylicense_attach where ID='" + lst_ID + "'";
OracleCommand cmd = new OracleCommand(filter, conn);
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader["DATA"] != DBNull.Value)
{
string ID = "数据ID对应值ID " + reader["ID"].ToString();
string length = "原文件大小" + reader["FILE_SIZE"].ToString();
SetInfo(ID, length);
System.IO.MemoryStream stream = new System.IO.MemoryStream((byte[])reader["DATA"]);//把照片读到MemoryStream里
Image ImageBlob = Image.FromStream(stream, true); //从数据库中读到的图片
int height = ImageBlob.Height; //原来图片的高度
int width = ImageBlob.Width;//原来图片的宽度
//开始进行图片的压缩
GetPicThumbnail1(ImageBlob, path, height, width, 60);
Image SaveImage = im; //得到返回来压缩之后的图片
byte[] buffer = imageToByteArray(SaveImage);
string UpdateSql = "update bs_buylicense_attach set DATA =:image where ID='" + lst_ID + "'";
OracleCommand cd = new OracleCommand();
cd = new OracleCommand(UpdateSql, conn);
cd.CommandText = UpdateSql;
cd.Parameters.Add("image", System.Data.OracleClient.OracleType.Blob, buffer.Length).Value = buffer;
cd.ExecuteNonQuery();
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
string YAsuo = Convert.ToString(file.Length);
SetInfo("压缩为:", YAsuo);
stream.Close();
stream.Dispose();
file.Dispose();
file.Close();
File.Delete(@path);
}
}
}
conn.Close();
string end = "所有数据执行完毕关闭数据库链接!" + DateTime.Now;
SetInfo("", end);
SaveLog();
label2.Text = "结束时间:" + DateTime.Now;
}

C. C#做c/s开发,是怎么读取远程主机的图像文件的

图片都存在服务器上, cs程序端也是通过URL形式访问图片,如System.Net.WebClient类可以读取远程图片, 本质上和BS结构一样,只不过BS结构是浏览器帮你实现读取远程图片这一步。

当然,服务器端得架设一个web站点提供服务。

D. C#怎样读取数据库中Image类型在线等!!

存进去了就可以用流文件读出来 然后绘制到屏幕上.
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 = 0; Ycount < MBitmap.Height; Ycount++)//双层循环,遍历bmp数组,分别调用SetPixel方法
{
for (int Xcount = 0; Xcount < MBitmap.Width; Xcount++)
{
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 = 0; Ycount < h; Ycount++)
{
for (int Xcount = 0; Xcount < w; Xcount++)
{
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类型图片的绘制,你可以看下

E. C#winform 中上传图片保存到数据库中

就是2中方法:
1:上传图片的相对路径到数据库中相应字段里,读取显示时,将控件(假设用的是Image控件)的ImageUrl属性指向该相对路径即可。

2:将图片以二进制流的方式整体上传到数据库里,读取显示时,以二进制流的方式整体读出。这种方法稍微麻烦一点,但保存的是图片整体到数据库里。

F. C#WinForm中,用于将图片以二进制存入sql数据库中,并将图片从数据库中取出,显示在PictureBox控件中。

插入: //单击图片选择添加的图片 private void pic_Click(object sender, EventArgs e)
{ dlg.Filter = "JPG|*.jpg|BMP|*.bmp|PNG|*.png";
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pic.Image = Image.FromFile(dlg.FileName);
txtFilePath = dlg.FileName;
}
} public byte[] picData; public string txtFilePath = ""; 添加确定按钮代码: f (txtFilePath != "")
{
try
{
FileStream fs = new FileStream(txtFilePath, FileMode.Open, FileAccess.Read);
int len = Convert.ToInt32(fs.Length);
b = new byte[len];
fs.Read(b, 0, len);
fs.Close();
}
catch
{
b = null;
}
} SqlConnection conn = new SqlConnection(strConn);
conn.Open(); SqllCommand cmdInsert = new SqlCommand();
cmdInsert.Connection = conn;
cmdInsert.CommandText =插入语句; cmdInsert.Parameters.Add("@照片", SqlDbType.Image); if (txtFilePath == "")
{
cmdInsert.Parameters["@照片"].Value = DBNull.Value;
}
else
{
cmdInsert.Parameters["@照片"].Value = b;
}
cmdInsert.ExecuteNonQuery();
conn.Close();获取: public byte[] picData; SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from 联系人 where 编号=" + ID.ToString();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet(); sda.Fill(ds); if (ds.Tables[0].Rows.Count == 1)
{
if (ds.Tables[0].Rows[0]["照片"] == DBNull.Value)
{ //pic为picturebox控件
pic.Image = PhoneBoook.Properties.Resources.DeskShade;//为空的话给个默认图片
}
else
{
byte[] b = (byte[])(ds.Tables[0].Rows[0]["照片"]);
pic.Image = Image.FromStream(new MemoryStream(b));
picData = b;
}
}

G. C#读取数据库IMAGE字段的内容。

读取... 读取长二进制为图片..
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=test05;database=esdb2;uid=datatran;pwd=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();
}

热点内容
scratch少儿编程课程 发布:2025-04-16 17:11:44 浏览:642
荣耀x10从哪里设置密码 发布:2025-04-16 17:11:43 浏览:368
java从入门到精通视频 发布:2025-04-16 17:11:43 浏览:89
php微信接口教程 发布:2025-04-16 17:07:30 浏览:311
android实现阴影 发布:2025-04-16 16:50:08 浏览:794
粉笔直播课缓存 发布:2025-04-16 16:31:21 浏览:346
机顶盒都有什么配置 发布:2025-04-16 16:24:37 浏览:213
编写手游反编译都需要学习什么 发布:2025-04-16 16:19:36 浏览:818
proteus编译文件位置 发布:2025-04-16 16:18:44 浏览:368
土压缩的本质 发布:2025-04-16 16:13:21 浏览:594