彩票走势图

DotImage使用教程:从数据库中读写图像

原创|使用教程|编辑:龚雪|2014-05-13 09:23:47.000|阅读 358 次

概述:本文主要介绍如何利用利用C#和VB.NET从数据库中读写DotImage图像。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:

.Net图像处理控件Atalasoft DotImage具有流媒体功能,可与ADO.NET共同使用以直接读取数据库中的图像,无需保存至临时文件。下面代码片段演示了如何利用C#和VB.NET从数据库中读写图像。

写入数据库:

C#

private void SaveToSqlDatabase(AtalaImage image)
{
SqlConnection myConnection = null;
try
{
// Save image to byte array.
byte[] imagedata = image.ToByteArray(new Atalasoft.Imaging.Codec.JpegEncoder(75));

// Create the SQL statement to add the image data.
myConnection = new SqlConnection(CONNECTION_STRING);
SqlCommand myCommand = new SqlCommand
("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection);
SqlParameter myParameter = new SqlParameter("@Image", SqlDbType.Image, imagedata.Length);
myParameter.Value = imagedata;
myCommand.Parameters.Add(myParameter);


// Open the connection and execture the statement.
myConnection.Open();
myCommand.ExecuteNonQuery();
}
finally
{
myConnection.Close();
}
}

Visual Basic.NET

Private Sub SaveToSqlDatabase(ByVal image As AtalaImage)
Dim myConnection As SqlConnection = Nothing
Try
' Save image to byte array.
Dim imagedata() As Byte = image.ToByteArray(New Atalasoft.Imaging.Codec.JpegEncoder(75))


' Create the SQL statement to add the image data.
myConnection = New SqlConnection(CONNECTION_STRING)
Dim myCommand As SqlCommand = New SqlCommand
("INSERT INTO Atalasoft_Image_Database (Caption, ImageData) VALUES ('" + txtCaption.Text + "', @Image)", myConnection)
Dim myParameter As SqlParameter = New SqlParameter("@Image", SqlDbType.Image, imagedata.Length)
myParameter.Value = imagedata
myCommand.Parameters.Add(myParameter)


' Open the connection and execture the statement.
myConnection.Open()
myCommand.ExecuteNonQuery()
Finally
myConnection.Close()
End Try
End Sub

从数据库中读取:

C#

private AtalaImage OpenFromSqlDatabase()
{
SqlConnection myConnection = null;
try
{
// Establish connection and SELECT statement.
myConnection = new SqlConnection(CONNECTION_STRING);
SqlCommand myCommand = new SqlCommand
("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection);
myConnection.Open();


// Get the image from the database.
byte[] imagedata = (byte[])myCommand.ExecuteScalar();
if (imagedata != null)
{
AtalaImage image = AtalaImage.FromByteArray(imagedata);
return image;
}
else
{
MessageBox.Show("Image does not exist in database.");
return null;
}
}
finally
{
myConnection.Close();
}
}

Visual Basic .NET

Private Function OpenFromSqlDatabase() As AtalaImage
Dim myConnection As SqlConnection = Nothing
Try
' Establish connection and SELECT statement.
myConnection = New SqlConnection(CONNECTION_STRING)
Dim myCommand As SqlCommand = New SqlCommand
("SELECT ImageData FROM Atalasoft_Image_Database WHERE Caption = '" + txtCaption.Text + "'", myConnection)
myConnection.Open()


' Get the image from the database.
Dim imagedata() As Byte = CType(myCommand.ExecuteScalar(), Byte())
If (Not imagedata Is Nothing) Then
Dim image As AtalaImage = AtalaImage.FromByteArray(imagedata)
Return image
Else
MessageBox.Show("Image does not exist in database.")
Return Nothing
End If
Finally
myConnection.Close()
End Try
End Function

 


标签:.NET图像处理

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP