How to manipulate image filed in a database?
Hi,
Thank you for using Microsoft Community Support.
Try the following code. The type of the pict field is 'image'.
I hope this helps. If you have any questions, please reply to this post.
Best Regards,
Jun Su
Microsoft Support
---
This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. Copy Right, 2001 Microsoft Corporation.
All rights reserved.
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents conn As System.Data.SqlClient.SqlConnection
Friend WithEvents comm As System.Data.SqlClient.SqlCommand
Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents comm2 As System.Data.SqlClient.SqlCommand
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.De{*filter*}StepThrough()> Private Sub
InitializeComponent()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.Button1 = New System.Windows.Forms.Button()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.conn = New System.Data.SqlClient.SqlConnection()
Me.comm = New System.Data.SqlClient.SqlCommand()
Me.PictureBox2 = New System.Windows.Forms.PictureBox()
Me.Button2 = New System.Windows.Forms.Button()
Me.comm2 = New System.Data.SqlClient.SqlCommand()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(608, 416)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'PictureBox1
'
Me.PictureBox1.Image =
CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Bitmap)
Me.PictureBox1.Location = New System.Drawing.Point(24, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(184, 104)
Me.PictureBox1.TabIndex = 1
Me.PictureBox1.TabStop = False
'
'conn
'
Me.conn.ConnectionString = "data source=sha-sujun-wks;initial
catalog=Northwind;integrated security=SSPI;pers" & _
"ist security info=False;workstation id=SHA-SUJUN-WKS;packet
size=4096"
'
'comm
'
Me.comm.Connection = Me.conn
Me.comm.Parameters.Add(New
System.Data.SqlDbType.VarBinary, 800000, "Pict"))
'
'PictureBox2
'
Me.PictureBox2.Location = New System.Drawing.Point(152, 88)
Me.PictureBox2.Name = "PictureBox2"
Me.PictureBox2.Size = New System.Drawing.Size(416, 248)
Me.PictureBox2.TabIndex = 2
Me.PictureBox2.TabStop = False
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(496, 416)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 3
Me.Button2.Text = "Button2"
'
'comm2
'
Me.comm2.CommandText = "SELECT TOP 1 Pict FROM Table1"
Me.comm2.Connection = Me.conn
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(696, 462)
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.Button2, Me.PictureBox2, Me.PictureBox1, Me.Button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim a As MemoryStream
conn.Open()
a = New MemoryStream(8000)
PictureBox1.Image.Save(a, ImageFormat.Jpeg)
Dim arr As Byte()
arr = a.ToArray()
MsgBox(arr.Length)
comm.ExecuteNonQuery()
conn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim a As MemoryStream
Dim o As Byte()
conn.Open()
o = comm2.ExecuteScalar()
a = New MemoryStream(o)
PictureBox2.Image = Image.FromStream(a)
conn.Close()
End Sub
End Class
--------------------
| Subject: How to manipulate image filed in a database?
| Date: Mon, 22 Oct 2001 22:34:38 +0800
|
| how to sava a image file to a image filed using ado.net and put these data
| into a picuturebox?
|
|
|