Results 1 to 19 of 19

Thread: saving image as bitmap

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73

    Unhappy 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.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    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????????????????

  3. #3
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    thanks,
    what do I put for the value (@Myimage)
    strSQL = "INSERT INTO MyTable (ImageField) VALUES (@MyImage)"

  5. #5
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    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

  7. #7
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    How do I fill the buffer?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    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.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73

    Unhappy

    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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    Does anyone have any suggestions to my above post?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    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?

  13. #13
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    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

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2004
    Posts
    73
    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.

  16. #16
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    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.

  17. #17
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. 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.

  18. #18
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    Or try/catch it.
    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.

  19. #19
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Try
    2.   'converting empty string to a number
    3.    AddItemNum = Integer.Parse(txtAddItemNumber.Text)
    4. Catch ex As Exception
    5.   'there was an error...  don't know 100 % what it was...
    6. 'so what do we do now?  Shh...  let's just pretend it didn't happen.
    7. 'At least there won't be a break in the application. Good... go with it.
    8. End Try
    9.  
    10. 'adjust lunar lander trajectory by AddItemNum
    11.  
    12. [b]BOOM! CRASH![/b]
    13.  '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
  •  



Click Here to Expand Forum to Full Width