|
Thanks for the info and the link! I'll try out.
Pamodh.
|
|
|
|
|
You're welcome
|
|
|
|
|
I tried it out and the Zip library works great!!
Thank you so much.
|
|
|
|
|
Hi there,
I have a form containing a listbox showing a list of image names. It's bound
to the database table. When an image name is clicked it shows the image and
imagename in a picturebox and textbox respectively. When no image is selected
in the listbox, a new record can be inserted by browsing a new image in the
picturebox by an openfiledialog, writing the imagename in the textbox and
pressing the OK button. When an image is already selected, the record can be
updated by pressing the same OK button. The data is saved into MSSQL Server
2005. Corresponding table fields are Keycode int autono,
logoname nvarchar(50), logo image.
Now the problem, when I insert a new data with an image everything goes fine
but whenever I try to update an existing data with an image it throws an
exception- 'A generic error occurred in GDI+.' at the following line-
'pic.Image.Save(ms, pic.Image.RawFormat)'. Surprisingly when I update an
existing data without any image in the picturebox no exception is generated.
I have crossed checked it and seems that the problem is just at one point-
'Updating the image from the picturebox'.
I'm almost done all throughout but stuck to this particular point. Please help. Regards.
My code to insert/update the data by OK button and to populate it by listbox
doubleclick follows:
Private ms As MemoryStream
Private arrImage() As Byte
Private conn As SqlConnection
Private cmd As SqlCommand
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Method to bind listbox.
BindListBox(lst, "Select Keycode,LogoName from tbltest", "Logoname", "keycode")
Tag = "Insert"
End Sub
Private Sub lst_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lst.DoubleClick
Dim dr As SqlDataReader
dr = CreateReader("Select LogoName,logo from tblTest where keycode=" & lst.SelectedValue)
If dr.Read Then
txtLogoName.Text = vbNullString & dr("Logoname")
If Not IsDBNull(dr("Logo")) Then
arrImage = CType(dr("Logo"), Byte())
ms = New MemoryStream(arrImage)
pic.Image = Image.FromStream(ms)
ms.Close()
Else
pic.Image = Nothing
pic.Invalidate()
End If
Tag = "Update"
End If
dr.Close()
closeconnection()
arrImage = Nothing
ms = Nothing
End Sub
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
Dim com As SqlCommand
Dim strSql As String
If Tag = "Insert" Then
strSql = "Insert into tbltest (logoname,logo) values ('" & Trim(txtLogoName.Text) & "',@Logo)"
Else
strSql = "Update tbltest set logoname='" & Trim(txtLogoName.Text) & "',Logo=@Logo Where keycode=" & lst.SelectedValue
End If
com = CreateCommand(strSql)
com.Parameters.Add(New SqlParameter("@Logo", SqlDbType.Image))
If Not pic.Image Is Nothing Then
ms = New MemoryStream()
pic.Image.Save(ms, pic.Image.RawFormat)
arrImage = ms.GetBuffer
ms.Close()
com.Parameters("@Logo").Value = arrImage
Else
com.Parameters("@Logo").Value = DBNull.Value
End If
If com.ExecuteNonQuery = 1 Then
closeconnection()
BindListBox(lst, "Select Keycode,LogoName from tbltest", "Logoname", "keycode")
pic.Image = Nothing
pic.Invalidate()
txtLogoName.Clear()
Tag = "Insert"
End If
arrImage = Nothing
ms = Nothing
strSql = Nothing
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
With dlg
.Filter = "All Files|*.*|Bitmap|*.bmp|GIF|*.gif|Icon|*.ico|JPEG|*.jpg|PNG|*.png"
.FilterIndex = 5
End With
If dlg.ShowDialog() = DialogResult.OK Then pic.Image = Image.FromFile(dlg.FileName)
End Sub
Public Sub setconnection()
Try
conn = New SqlConnection("Data Source=MyServer;Initial Catalog=TestDB;User Id=sa;Password=;")
conn.Open()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub closeconnection()
conn.Close()
End Sub
Public Function CreateCommand(ByVal query As String) As SqlCommand
setconnection()
Dim command As New SqlCommand(query, conn)
Return command
End Function
Public Function CreateReader(ByVal query As String) As SqlDataReader
Dim reader As SqlDataReader
setconnection()
cmd = CreateCommand(query)
reader = cmd.ExecuteReader()
Return reader
End Function
|
|
|
|
|
Hi,
first of all, congrats on a great post. This is how it should be done!
second, it is unfortunate however almost all problems inside GDI/GDI+ cause the same error message to appear and Image.Save() can fail for a whole lot of reasons (access denied, disk full, you name it).
I didn't quite follow all the possible sequences of your logic, however this would be my best guess:
- the file you want to save to is "in use";
- probably because you did open it for creating an Image object, either directly, as in Image.FromFile(); or indirectly as in new Stream(...) and Image.FromStream(). The file would be locked until the image object gets disposed of.
- there isn't really a way to free the file while you still need the image, the Image class needs access to it in case you're going to manipulate the metadata.
- my typical work-around is to never just do Image.FromFile(); instead I do:
Image temp=Image.FromFile(filepath)
Bitmap bm=new Bitmap(temp)
temp.Dispose()
Disposing is what frees the file; using a "copy constructor" for a second image object breaks the connection between the remaining image and the original file.
Hope this helps.
BTW: your code is probably lacking some Dispose() calls anyway; <cod>pic.Image=Nothing is one way of potentially throwing away the last reference to an image without disposing it, causing it to linger around longer than necessary which isn't a good idea as images are rather large objects, and they often lock a file.
|
|
|
|
|
I have found the root of the problem but still couldn't get any solution. When we save the image using this line- 'pic.Image.Save(ms, pic.Image.RawFormat)' the image needs to be in its original format for the RawFormat() method to work properly. So when we click the Browse button, it's browsing an image into the picturebox in its original format. But when we retrieve the image from the database using arrays and streams, though we can view it but it's not in its original format and hence the above mentioned line in btnOk is throwing the exception.
That's exactly the point and that's why inserting a new image is ok, update with a different image is ok but update with the same image is crashing. I think the key to solve it is while retrieving the image from the database if we can somehow convert it to its original format or some format that's recognizable by the RawFormat() method and then show it in the picturebox. In that way when we press btnOk for update, RawFormat() will get the original format of the picture and save it properly.
I don't know how to do it practically or if there's some workaround. Can anybody make a solution out of this? Please assess on this topic and give your views. Regards.
|
|
|
|
|
Why do you insist on using RawFormat at all?
What people most often do when keeping an image in the database, is saving the image to a byte[], then the byte[] to a blob field; AND often also storing the filename so an appropriate file could be re-created.
Maybe you should read a few CodeProject articles on the subject.
|
|
|
|
|
Hello Everybody,
I want to know that how the Barcode Reader Read Barcode Image?
Thanks
If you can think then I Can.
|
|
|
|
|
You scan the the item with a bar-code using a bar-code reader. The reader picks up reflected light and converts it into a code. The code is output as an electrical signal so it can be interpreted by other devices such as a PC.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
|
hi,
i have a an unbound datagridview, i will load the datagridview programatically by clickng a button.
which is not a problem, the isssue is when i add some data to the new added row, because i have a button
which can able to add a new row. on that new row, i will add new data, when i hit the enter key, the new added
data will go to the top or to the first row which is a bug. it should not be copied to the first row. its
weird because i dont have the function to copy.
thanks is advance
C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
This is a snippet for when I fill an unbound datagrid. The row is appended at the bottom of the dgv.
_poItemDT.Rows.Add(New Object() { _
_curPOMaterial.Id, _
Double.Parse(txtOrderQty.Text).ToString("#,##0.000"), _
_curPOMaterial.UOM, _
_curPOMaterial.MaterialName, _
_curPOMaterial.Decsription, _
Double.Parse(txtSalePrice.Text).ToString("0.0000"), _
(CDbl(txtOrderQty.Text) * (CDbl(txtSalePrice.Text))).ToString("0.0000")})
Edit - on further reflection (and without seeing your code) I guess you are creating a DataGridViewRow and then adding that to the DataGridView. I would create a DataTable as above, populate the DataTable's rows, and then set the DGV.DataSource = dt
I don't speak Idiot - please talk slowly and clearly
'This space for rent'
Driven to the arms of Heineken by the wife
|
|
|
|
|
Hi,
I am trying to get a program, created in visualbasic.net 2008 on a windows xp computer, to work on a computer with windows 2000 installed on it. Is this possible?
Thanks!
|
|
|
|
|
Sure, just install the .NET Framework you wrote the app against and install your app.
|
|
|
|
|
|
|
Is there any way to detect selected text in another window. As a example there is a form with a text box and it's minimized in the system tray. When a user select a text in a window or another program(like word, excel etc.) that minimized form should appear and selected text should place in the text box. If this is possible, please someone give me an idea to do this.
|
|
|
|
|
Asked and answer many times.
There is no reliable method to detect text being selected in every application. You'd have to write methods for generic methods you could TRY on selected apps, but there is nothing that will work in all cases. Also, there are apps where no matter what you do you couldn't detect this at all.
|
|
|
|
|
Yes, this is possible in most circumstances.
The first problem is in identifying which window you care about. Multiple applications can have text selected at the same time. You can make clever use of FindWindow() and FindWindowEx() to get a handle to the window in question.
The second problem is in identifying the GUI element on that window that might have text that you are interested in. This is actually a much more complicated problem since the GUI element could be any sort of object, nested anywhere on the window. You could use GetWindow() to help with this.
The third problem is telling what text is selected within that GUI element. The way you do this depends on the GUI element in question. In a few cases, it may not even be possible. I'm not really sure where I would begin to try to crack that nut.
There are a ton of useful API calls that you can use at http://msdn.microsoft.com/en-us/library/ff468919%28v=VS.85%29.aspx[^]
You might almost be better off thinking about this problem differently. You could, for example, set up some global system hooks to capture the relevant mouse and keyboard events that occur when text is selected. Then, you would have immediate and direct insight into the objects with which you were dealing. Read this article for an idea of where to start with that approach: Global System Hooks in .NET[^]
Good luck.
|
|
|
|
|
Hi, dear all,
I have several textbox in my form.
Among them there are two have dependency.
For example (not same but the idea is same), user need to input name, age and action.
I add Leave event for age textbox, if age < 18 years old, the action dropdown box will be disabled.
Everything is fine if I move to other control after input age, the action dropdown box will be enable/disabled at once I leave the textbox.
My problem is that if I input age and didn't click other control in the form, instead I click the form itself, the Leave event won't get fired, so the action dropdown box cannot be updated (enable or disable).
What should I do to always have the Leave event fired?
Thanks!
|
|
|
|
|
Andraw Tang wrote: What should I do to always have the Leave event fired?
Leave the textbox?
More seriously, if you check you will see that if a textbox has the focus, when you click on the form, the box does not lose focus.
So you need to detect the click on the form, and if the textbox currently has focus then leave it.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
|
David,
Thanks for your reply.
Yes, you are right, even we click the form, the textbox still has focus. I have several textbox, how can I detect which one is focused? do you have some example about it?
|
|
|
|
|
|
Also if I put these textbox in a Groupbox in a form, when you click the Groupbox, the form.click won't be fired, in this case Do I need to right click event for all these container?
|
|
|
|
|
Write a single eventhandler, and hook up all the events that need be handled. Means one actual implementation, and the other handlers pointing to that implementation.
I are Troll
|
|
|
|
|
Would you please give me an example how to use it?
Thanks!
|
|
|
|
|