关于数据库中图形的存储与显示的问题

triout 2002-05-26 02:25:50
我在开发WINDOWS应用的时候,碰到一个难题:

通过FILESTREAM把图形数据读到了PICTUREBOX控件,该如何把PICTUREBOX控件中IMAGE数据转换BYTE[]类型?

存储在数据库二进制数据字段中的数据读取后,又该如何把它转换成BYTE[]类型?

谢谢!!
...全文
41 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
klxyz 2002-05-31
  • 打赏
  • 举报
回复
把你的代码贴出来
triout 2002-05-30
  • 打赏
  • 举报
回复
能保证该数据是存在的。

我是通过ASPX文件把字段中图形输出的,如我所预期的,但使用C#就报告了错误。
acptvb 2002-05-30
  • 打赏
  • 举报
回复
感谢您使用微软产品。

问题可能是您的数据表中的image字段的数据内容存在问题,建议先通过上面的步骤:实现将image文件写入数据表的image字段,然后再进行读取。
上述代码在如下的测试环境中运行通过:
数据库:MS SQL Server 2000
数据表students结构如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[students]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[students]
GO

CREATE TABLE [dbo].[students] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[age] [int] NULL ,
[image] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
开发环境:
Windowx XP Professional
Visual Studio.Net 2002 Version 7.0.9466
Microsoft .NET Framework 1.0 Version 1.0.3705

— 微软全球技术中心 VB支持中心

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
triout 2002-05-29
  • 打赏
  • 举报
回复
上面的:
myData = (byte[])myRow["image"];
报告说:
指定的转换无效,为什么?
bolar 2002-05-29
  • 打赏
  • 举报
回复
up
acptvb 2002-05-28
  • 打赏
  • 举报
回复
感谢您使用微软产品。

C#里获取SQL数据表的IMAGE类型字段内容(到PictureBox控件):
1,获取image字段的值,并转化为byte[]数组;
2,然后建立一个FileStream对象,并将byte[]数值写到一个临时的图象文件;
3,关闭FileStream对象,并在PictureBox控件中显示获得的临时图象文件。
下面提供一段示例代码,供您参考:
private void btnRetrieve_Click(object sender, System.EventArgs e)
{
// Fill a DataSet
DataSet ds = new DataSet();
string connString = "Server=SHA-RICKIE-01;DataBase=test;uid=user;pwd=user";
string sqlString ="Select * from Students";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlString,conn);

sqlDataAdapter1.Fill(ds, "students");
DataRow myRow;
myRow = ds.Tables["students"].Rows[0];
byte[] myData = new byte[0];
if(myRow["image"].ToString()!="")
myData = (byte[])myRow["image"];
else
{
MessageBox.Show("The image field is null.");
return;
}
int ArraySize = new int();
ArraySize = myData.GetUpperBound(0);

// Create a Filestream for writing the byte array to a image file
FileStream fs = new FileStream("tmpImage", FileMode.OpenOrCreate, FileAccess.Write);
// Write the stream of data that we read in from the database to the filestream
// and close it.
// This will create the file tmpImage, which we can use to display in the picturebox
// Writes a block of bytes to this stream using data from a buffer
fs.Write(myData, 0,ArraySize+1);
fs.Close();

// Assign the temporary image file to the picture box
pictureBox1.Image = new Bitmap("tmpImage");
}


— 微软全球技术中心 VB支持中心

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
acptvb 2002-05-28
  • 打赏
  • 举报
回复
感谢您使用微软产品。

在C#中,可以通过如下的步骤来实现将image文件写入数据表的image字段:
1,创建一个FileStream对象,通过该FileStream对象来读取指定的image文件;
2,根据生成FileStream对象,创建byte[]数据,通过FileStream对象的Read方法将数据写入byte[]数组中;
3,将上述byte[]数组赋予数据表的image字段,写入数据表中;

下面提供一段示例程序,供您参考:
private void btnAdd_Click(object sender, System.EventArgs e)
{
// Fill a DataSet
DataSet ds = new DataSet();
string connString = "Server=SHA-RICKIE-01;DataBase=test;uid=user;pwd=user";
string sqlString = "Select * from Students";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlString,conn);
SqlCommandBuilder MyCB = new SqlCommandBuilder(sqlDataAdapter1);

sqlDataAdapter1.Fill(ds, "students");
DataTable TheTable = ds.Tables[0];
// Create a new row in Memory
DataRow aRow = TheTable.NewRow();
// Insert the information from the dialog into the Table
aRow["name"] = txtboxname.Text;
aRow["age"] = txtboxage.Text;
// 创建一个FileStream对象用来读取image文件,其中FileName 为该image的完整文件名
string FileName = txtboxFilename.Text;
FileStream fs = new FileStream (FileName, FileMode.OpenOrCreate,
FileAccess.Read);
// Read the Data into the Byte Array
byte[] MyData = new byte[fs.Length];
fs.Read(MyData, 0, (int)fs.Length);
fs.Close();
// Assign the DataRow Picture Column to the Byte Array to populate it in the DataRow
aRow["image"] = MyData;
// Add the DataRow to the DataTable
TheTable.Rows.Add(aRow);
// 写数据记录到数据库表中
sqlDataAdapter1.Update(ds, "students");
MessageBox.Show("A student added.");
}


关于FileStream类的更详细信息,请参考MSDN:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemIOFileStreamMembersTopic.htm

— 微软全球技术中心 VB支持中心

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
triout 2002-05-26
  • 打赏
  • 举报
回复
谢谢楼上的!

现在的问题是:我把数据读取到DS中,该数据是ds.Tables[0].Rows[i]["照片"],我在WEB成功的应用了你上面的方法,但在WIN应用中却说不能实现转变数据类型。
bpfrom 2002-05-26
  • 打赏
  • 举报
回复
这篇文章肯定可以解决这个问题:
http://www.aspcool.com/lanmu/browse1.asp?ID=830&bbsuser=asp
lonk 2002-05-26
  • 打赏
  • 举报
回复
串行化吧。
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatter.Binary;

MemoryStream ms=new MemoryStream();
BinaryFormatter bf=new BinaryFormatter();

Image a=pictureBox1.Image;
bf.Serialize(ms,a); // 串行化

ing length=ms.Length; // 转化成byte[]
byte[] imagebyte=new byte[length];
imagebyte=ms.GetBuffers();

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧