|
-
Jul 1st, 2004, 08:43 AM
#1
Thread Starter
Lively Member
saving image as bitmap
I have a drawing program on frmdraw. I want to save what the user draws to the database. I guess saving it as a bitmap. Would my first step be to add a picturebox on my form? If so do I put my code for drawing in the picturebox?
Last edited by pea33nut; Jul 8th, 2004 at 02:44 PM.
-
Jul 1st, 2004, 02:34 PM
#2
Thread Starter
Lively Member
In this form I have a program that will draw on the the form when ran. I need to save the image in my sql database. I have a menu at the top right hand corner that has a submenu called save. here is my code.
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class frmdraw
Inherits System.Windows.Forms.Form
' enums....
Public Enum GraphicsTools As Integer
CirclePen = 0
End Enum
Public Enum GraphicsSizes As Integer
Small = 4
Medium = 10
Large = 20
End Enum
' members.....
Public GraphicsItem As New ArrayList
Public GraphicTool As GraphicsTools = GraphicsTools.CirclePen
Public GraphicsSize As GraphicsSizes = GraphicsSizes.Large
Public GraphicColor As Color = Color.Black
' DoMousePaint - respond to a mouse movement....
Private Sub DoMousePaint(ByVal e As MouseEventArgs)
' store the new item somewhere...
Dim newItem As GraphicsItem
' what tool are you using?
Select Case GraphicTool
' circlepen?
Case GraphicTool.CirclePen
' create a new graphics circle...
Dim circle As New GraphicsCircle
circle.SetPoint(e.X, e.Y, GraphicsSize, GraphicColor, True)
' store this for addition...
newItem = circle
End Select
' where you given an item?
If Not newItem Is Nothing Then
' add it to the list...
GraphicsItem.Add(newItem)
'invalidate...
Invalidate()
End If
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
' is the button down?
If e.Button = MouseButtons.Left Then
DoMousePaint(e)
End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
' is the button down?
If e.Button = MouseButtons.Left Then
DoMousePaint(e)
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
' go through the list...
Dim item As GraphicsItem
For Each item In GraphicsItem
'ask each item to draw itself
item.Draw(e.Graphics)
Next
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''this is my save menu''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
Dim bmp As New Bitmap(300, 300)
Dim bmpRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = SmoothingMode.HighQuality
g.FillRectangle(New Drawing2D.LinearGradientBrush(bmpRect, Color.White, Color.SlateGray, 50), bmpRect)
Dim x, y As Double
For degree As Integer = 0 To 359 Step 15
x = Math.Sin((degree / 180) * Math.PI) * 35 + bmp.Width / 2
y = Math.Cos((degree / 180) * Math.PI) * 35 + bmp.Height / 2
g.DrawEllipse(Pens.Black, New RectangleF(CSng(x) - 50, CSng(y) - 50, 100, 100))
Next
g.DrawString("created by GDI+ & VB.NET", Font, Brushes.Black, 2, 2)
g.DrawRectangle(Pens.Red, 0, 0, bmp.Width - 1, bmp.Height - 1)
g.Dispose()
Dim memStrm As New IO.MemoryStream
bmp.Save(memStrm, Imaging.ImageFormat.Jpeg)
bmp.Dispose()
Dim buffer() As Byte = memStrm.ToArray()
'do something to insert this byte-buffer in to a
'Bit(Byte?)Array-column (I guess that shoud be the right type) of a new DataRow...
'just for testing: save the file to disk:
Dim fs As IO.FileStream = IO.File.Create("test.jpg")
fs.Write(buffer, 0, buffer.Length)
fs.Flush()
fs.Close()
End Sub
End Class
where do i go from here????????????????
-
Jul 1st, 2004, 03:28 PM
#3
Frenzied Member
You need an image field in your SQl Server Database, and then just do an insert. I haven't tested this code, but it should be something like this...
From this line of code, something like...
Dim buffer() As Byte = memStrm.ToArray()
' Construct INSERT Command
Dim sqlCommand as new sqlClient.SqlCommand
Dim strSQl as String
strSQL = "INSERT INTO MyTable (ImageField) VALUES (@MyImage)"
sqlCommand.Parameters.Add("@MyImage", SqlDbType.Image, buffer.Length).Value = buffer
' I'm gonna assume you can setup a SQL Connection
SQLConn.Open()
sqlCommand.ExecuteNonQuery()
SQLConn.Close()
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 1st, 2004, 03:53 PM
#4
Thread Starter
Lively Member
thanks,
what do I put for the value (@Myimage)
strSQL = "INSERT INTO MyTable (ImageField) VALUES (@MyImage)"
-
Jul 1st, 2004, 04:00 PM
#5
Frenzied Member
It doesn't matter, that is just a parameter name. It just has to have the @ and match this line:
sqlCommand.Parameters.Add("@MyImage", SqlDbType.Image, buffer.Length).Value = buffer
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 5th, 2004, 03:19 PM
#6
Thread Starter
Lively Member
Im getting any error with my SqlCommand.ExecuteNonQuery()
Dim SQLConn As SqlConnection = New SqlConnection
Dim strSQl As String
Dim sqlCommand As New SqlClient.SqlCommand
SQLConn.ConnectionString = "Data Source=(local);" & _
"Initial Catalog=Justin;" & _
"Integrated Security=SSPI"
strSQl = "INSERT INTO [Auto] (ItemImage) VALUES (@MyImage)"
SqlCommand.Parameters.Add("@MyImage", SqlDbType.Image, buffer.Length).Value = buffer
' Open SQL Connection
SQLConn.Open()
SqlCommand.ExecuteNonQuery()
SQLConn.Close()
End Sub
-
Jul 6th, 2004, 08:20 AM
#7
Frenzied Member
What does your code look like to fill the buffer?
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 6th, 2004, 08:28 AM
#8
Thread Starter
Lively Member
How do I fill the buffer?
-
Jul 6th, 2004, 10:34 AM
#9
Thread Starter
Lively Member
Does anyone know how I fill the buffer. This is my first time dealing with buffers and I can't seem to find the answer.
PLEASE HELP
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
Dim bmp As New Bitmap(300, 300)
Dim bmpRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = SmoothingMode.HighQuality
g.FillRectangle(New Drawing2D.LinearGradientBrush(bmpRect, Color.White, Color.SlateGray, 50), bmpRect)
Dim x, y As Double
For degree As Integer = 0 To 359 Step 15
x = Math.Sin((degree / 180) * Math.PI) * 35 + bmp.Width / 2
y = Math.Cos((degree / 180) * Math.PI) * 35 + bmp.Height / 2
g.DrawEllipse(Pens.Black, New RectangleF(CSng(x) - 50, CSng(y) - 50, 100, 100))
Next
g.DrawString("created by GDI+ & VB.NET", Font, Brushes.Black, 2, 2)
g.DrawRectangle(Pens.Red, 0, 0, bmp.Width - 1, bmp.Height - 1)
Dim memStrm As New IO.MemoryStream
bmp.Save(memStrm, Imaging.ImageFormat.Jpeg)
Dim buffer() As Byte = memStrm.ToArray()
' connect to the database
Dim SQLConn As SqlConnection = New SqlConnection
Dim strSQl As String = strSQl = "INSERT INTO [Auto] (ItemImage) VALUES (@MyImage)"
SQLConn.ConnectionString = "Data Source=(local);" & _
"Initial Catalog=Justin;" & _
"Integrated Security=SSPI"
Dim MyCommand As New SqlCommand
MyCommand.CommandText = strSQl
MyCommand.Connection = SQLConn
SQLConn.Open()
' Construct INSERT Command
MyCommand.Parameters.Add("@MyImage", SqlDbType.Image, buffer.Length).Value = buffer
MyCommand.ExecuteNonQuery()
SQLConn.Close()
bmp.Dispose()
End Sub
Last edited by pea33nut; Jul 6th, 2004 at 10:58 AM.
-
Jul 6th, 2004, 02:04 PM
#10
Thread Starter
Lively Member
I have just changed my code a little. I have been working on this for a very long time and can't get it to work. The error im getting is from this line of code. MyCommand.ExecuteNonQuery()
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.
Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click
' convert image into bitmap
Dim bmp As New Bitmap(300, 300)
Dim bmpRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.SmoothingMode = SmoothingMode.HighQuality
g.FillRectangle(New Drawing2D.LinearGradientBrush(bmpRect, Color.White, Color.SlateGray, 50), bmpRect)
Dim x, y As Double
For degree As Integer = 0 To 359 Step 15
x = Math.Sin((degree / 180) * Math.PI) * 35 + bmp.Width / 2
y = Math.Cos((degree / 180) * Math.PI) * 35 + bmp.Height / 2
g.DrawEllipse(Pens.Black, New RectangleF(CSng(x) - 50, CSng(y) - 50, 100, 100))
Next
g.DrawString("created by GDI+ & VB.NET", Font, Brushes.Black, 2, 2)
g.DrawRectangle(Pens.Red, 0, 0, bmp.Width - 1, bmp.Height - 1)
'************************************************************************************
'************************************************************************************
' create a memorystream
Dim ms As MemoryStream = New MemoryStream
' create buffer to hold data
bmp.Save(ms, Imaging.ImageFormat.Jpeg)
Dim buffer(CType(ms.Length, Int32) - 1) As Byte
ms.Position = 0
ms.Read(buffer, 0, CType(ms.Length, Int32))
' connect to the database
Dim SQLConn As SqlConnection = New SqlConnection
SQLConn.ConnectionString = "Data Source=(local);" & _
"Initial Catalog=Justin;" & _
"Integrated Security=SSPI"
SQLConn.Open()
Dim strSQl As String = strSQl = "INSERT INTO [Auto] (ItemImage) VALUES (@MyImage)"
Dim MyCommand As New SqlCommand
MyCommand.CommandText = strSQl
MyCommand.Connection = SQLConn
Dim param As New SqlParameter(("@MyImage"), SqlDbType.Image, buffer.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, buffer)
MyCommand.Parameters.Add(param)
MyCommand.ExecuteNonQuery()
SQLConn.Close()
bmp.Dispose()
End Sub
End Class
Last edited by pea33nut; Jul 8th, 2004 at 12:39 PM.
-
Jul 8th, 2004, 12:42 PM
#11
Thread Starter
Lively Member
Does anyone have any suggestions to my above post?
-
Jul 9th, 2004, 09:06 AM
#12
Thread Starter
Lively Member
I ran the catch program and this is the error that I got. Line1: incorrect syntax near 'false'. Well this line here---
Dim strSQl As String = strSQl = "INSERT INTO [Auto] (ItemImage) VALUES (@MyImage)"
strSQL is = to 'false' when I run my break points. When the user types in a value in a text box and clicks the add button, a new form appears with the drawing program. I have a menu at the top with the save menu in it. My question is when the user types in a value on the previous form and clicks the add button, it saves it to my database. On my drawing form how does it know to save it to that same row in the database. Is that part of my problem?
-
Jul 9th, 2004, 09:42 AM
#13
Frenzied Member
If you already have a row, you need to do an update with some kind of value so it knows which row to put it in:
Dim strSQl As String = "UPDATE [Auto] SET ItemImage = @MyImage WHERE MyField = @Something"
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 9th, 2004, 12:05 PM
#14
Thread Starter
Lively Member
I think Im almost there. check out this line of code..
Dim strSQl As String = "UPDATE [Auto] SET ItemImage = @MyImage WHERE AutoId = ???"
I need the ??? to be = to the last autoid. What is the sql code to do that
-
Jul 9th, 2004, 12:09 PM
#15
Thread Starter
Lively Member
or better yet how so a make a variable so it can be used on different forms.
AddItemNum = Val(txtAddItemNumber) says it can't be converted to a textbox?
Last edited by pea33nut; Jul 9th, 2004 at 12:15 PM.
-
Jul 9th, 2004, 02:27 PM
#16
Frenzied Member
Wrap it in a class and just pass it between forms by reference. Or make it visible on a form and pass the form's reference around.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 9th, 2004, 03:10 PM
#17
I wonder how many charact
AddItemNum = Val(txtAddItemNumber) says it can't be converted to a textbox?
If that's your exact code, then you are trying to tell the compiler to set AddItemNum equal to result of the Val function, to which you pass the object txtAddItemNumber which is a TextBox... you don't want to pass a textbox to the Val function, you want to pass a string... as in... txtAddItemNumber.Text
In reality, this code should replace that entire line... because Val is a VB6 compatibility function and everyone frowns on that..
VB Code:
AddItemNum = Integer.Parse(txtAddItemNumber.Text)
Note, that if the textbox contains an empty string (String.Empty), then that will fail horribly (can't convert an empty string to a number), so you might want to check if there is actually text in the textbox, and make sure its only numbers.
Last edited by nemaroller; Jul 9th, 2004 at 03:14 PM.
-
Jul 9th, 2004, 03:19 PM
#18
Frenzied Member
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jul 9th, 2004, 10:29 PM
#19
I wonder how many charact
VB Code:
Try
'converting empty string to a number
AddItemNum = Integer.Parse(txtAddItemNumber.Text)
Catch ex As Exception
'there was an error... don't know 100 % what it was...
'so what do we do now? Shh... let's just pretend it didn't happen.
'At least there won't be a break in the application. Good... go with it.
End Try
'adjust lunar lander trajectory by AddItemNum
[b]BOOM! CRASH![/b]
'AH F@!#
Last edited by nemaroller; Jul 9th, 2004 at 10:33 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|