How to put noone image in image column of datagridview - vb.net

I have to put an image to specific column of datagridview but not in every row.
If getExists(myTable, CInt(reader.GetValue(0)), dbConn) Then
.Cells("myCol").Value = Image.FromFile("C:\myPath\myIco_16x16.ico")
Else
.Cells("myCol").Value = "" ' error here
End If
With this code I get error while executing probably because I'm try to put a string in image column. Second I try is:
.Cells("myCol").Value = Nothing
This don't cause error but put "error image" picture (with red X) to grid.
Is here a way to put no one image (blank) to datagridview's image column without loading a "blank image" from file or resource?

I've had to do this in the past and just used a 1 pixel transparent PNG. The performance was acceptable in my application even with 18 image columns and ~2000 rows and the background color of the cell shows through it just fine.
You should be able to create a 1x1 pixel transparent PNG fairly easily using a free program such as "Paint.Net".

You could just create a temporary blank bitmap and assign this to rather than loading an image from file.
Friend Function BlankImage() As Image
Try
Dim oBM As New Bitmap(1, 1)
oBM.SetPixel(0, 0, Color.Transparent)
Return oBM
Catch ex As Exception
Return Nothing
End Try
End Function

Slightly better method that can improve performance:
Friend Function BlankImage() As Image
Static oBM As New Bitmap(1, 1)
Try
If oBM Is Nothing Then
oBM.SetPixel(0, 0, Color.Transparent)
End If
Return oBM
Catch ex As Exception
Return Nothing
End Try
End Function

Related

add image to a Toolstripbutton from image

I have a small issue with my taskbar.The main idea is to create my own taskbar. And I would like to get the application icon to the ToolbarButton
I am using this code
ToolStrip2.Items.Clear()
For Each proc As Process In Process.GetProcesses
Try
ImgList.Images.Add(Icon.ExtractAssociatedIcon(proc.MainModule.FileName))
If proc.MainWindowTitle <> "" Then
Dim menuitem As New ToolStripButton() With
{
.Text = proc.MainWindowTitle,
.Image = ImgList.Images.Count - 1 **//The Error is here**
}
ToolStrip2.Items.Add(menuitem)
End If
Catch ex As Exception
End Try
Next
And I get this error "Value of type 'Integer' cannot be converted to 'Image'"
I know what does it mean, but I dont know how to solve the problem.
Thanks for Help
Use :
ImgList.Images(ImgList.Images.Count - 1)
Remember image of toolstrip must be image not integer!

How could I resolve A generic error occurred in GDI+. in VB.Net?

When image field property in class is set with the image raw format of the same image from my database in picture box, it always throws this exception. In contrast, if the image in the picture box has been updated with one I select from my local PC directory, the update function work fine.
Below is my code:
Try
With mEmployee
If Miscellaneous.GetImageName(ofdPhoto).ToLower = "No_Photo.jpg".ToLower Then
.Image = Nothing
Else
Dim stream As New MemoryStream
pbImage.Image.Save(stream, pbImage.Image.RawFormat)
.Image = stream.GetBuffer()
End If
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Now everything has solved. This GDI+ generic error caused by the picture box itself. Actually, When I bound the record with image from the database and update that record without updating the image in the PictureBox, the image field has been set with old byte() data from the box, and that caused the error.
To solve this, I have declare a byte() type variable to store the temp image byte() data from the database, and when update, if the image has not been changed it will be set with the data from that variable.
This is my code that solve everything:
Try
If Miscellaneous.GetImageName(ofdPhoto).ToLower = "No_Photo.jpg".ToLower Then
.Image = Nothing
Else
If isImageChanged = True Then
Dim stream As New MemoryStream
pbImage.Image.Save(stream, pbImage.Image.RawFormat)
.Image = stream.GetBuffer()
isImageChanged = False
ElseIf isRemoveImage = True Then
.Image = Nothing
isRemoveImage = False
Else
.Image = tempImage
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

How to save changes to my orginal image in vb.net?

I'm using this code to save image
PictureBox1.Image.Save(filePath)
after that I crop image and I want to save it again by replacing the old one with the new cropped one
any help please
regards,,,
Use the following code when you load the image into the PictureBox instead of the one you are currently using and you'll be fine just saving later. The using statement ensures the file is released once the image is loaded.
Replace filePath with your own.
Using stream as new FileStream(filePath, FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromStream(stream)
End Using
EDIT
From your last comment I can see this code
Try
Me.Opacity = 0%
Me.PictureBox1.Image = cc()
PictureBox1.Image.Save(filePath)
source = Image.FromFile(filePath)
PictureBox1.Image = source
TextBox1.Text = filePath
Me.Opacity = 100%
Catch ex As Exception
MsgBox(ex.Message)
End Try
You don't need to reload the image to the PictureBox after saving. Just get rid of the following lines.
source = Image.FromFile(filePath)
PictureBox1.Image = source
That should solve your problem at least for now as you won't be saving the image to the same image you load it from (you didn't actually load it). But you will have to find a better solution for your whole algorithm later :)
This's the answer
Dim bmp1 As New Bitmap(PictureBox1.Image)
If System.IO.File.Exists(filePath) Then
System.IO.File.Delete(filePath)
End If
bmp1.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg)
' Dispose of the image files.
bmp1.Dispose()

Crystal Report formula field index changing on each new build

I've got a Crystal Report which I made in CR2008, which I'm printing from my vb.net application. In this report, I have an image and a formula field.
The image formula (Under Format Graphic > Picture > Graphic Location is set to be the formula field called #imgLocation.
In my vb.net code, I have the following block to select the saved file path from the database, then fill #imgLocation with this value, to set the image that is being shown.
Try
Dim logoPath As String = ds.Tables(0).Rows(0).Item("reportLogoPath") & ""
If logoPath = "" Then
MessageBox.Show("No logo path has been defined in the System Settings. Please set " & _
"one and try again.", "Load Report Failed", MessageBoxButtons.OK)
Exit Sub
Else
cReport.DataDefinition.FormulaFields.Item(2).Text = Chr(34) & logoPath & Chr(34)
End If
Catch ex As Exception
errorLog(ex)
End Try
I've seen this working twice. The image I want replaced the placeholder image when I had FormulaFields.Item(2), but then I resized the image and it didn't work. I then changed the value of the index, and it worked with FormulaFields.Item(4), but again stopped working when I resized the image.
Why is the index changing, and what is the way around it?
I tried using the named parameter option, but FormulaFields.Item("#imgLocation") gave me an error of
Invalid Index
EDIT
As per #Bugs request, this is the report showing the correct string and image
Then, after deleting and re-saving the image (But in a slightly bigger size), the parameter was passed correctly still, but the placeholder image was shown instead.
This makes me think now that it's to do with image size, however, re-sizing it back to the original size still didn't show it again.
EDIT 2
Could it be do to with the fact that I'm loading a new form which contains the report viewer, rather than opening it from the same form?
cReport.RecordSelectionFormula = selectionFormula
Me.Cursor = Cursors.Default
Dim f As New frmReportViewer(con, cReport, 0)
f.Show()
Private Sub frmReportViewer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
setFormSizes(Me, con)
Me.Location = New Point((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2), 10)
Me.Text = "Report Viewer"
Dim logOnInfo As New TableLogOnInfo()
Dim i As Integer
For i = 0 To cReport.Database.Tables.Count - 1
logOnInfo.ConnectionInfo.DatabaseName = "comm_db"
logOnInfo.ConnectionInfo.Password = "Acplus2016!"
cReport.Database.Tables.Item(i).ApplyLogOnInfo(logOnInfo)
Next i
cReport.VerifyDatabase()
crViewer.ReportSource = cReport
crViewer.ToolPanelView = CrystalDecisions.Windows.Forms.ToolPanelViewType.None
crViewer.Zoom(87)
Catch ex As Exception
errorLog(ex)
End Try
End Sub
Please note my answer is addressing the part regarding adding parameters. For the moment the no image available placeholder is proving difficult to resolve on my side.
To add a parameter please follow these steps:
Add a parameter within Crystal Reports called imageLocation:
After pressing OK you should have a parameter in your list like this:
The following code will pass a value to the parameter:
Dim cReport As New ReportDocument
cReport.Load("C:\Report.rpt")
If cReport.ParameterFields.Item("imageLocation") IsNot Nothing Then
cReport.SetParameterValue("imageLocation", "\\SAGE200FS\Sage\StockImages\sc002.jpg")
End If
reportViewer.ReportSource = cReport
This is the output (I have added the parameter onto the report):
Note that if you are passing parameters and they are shown on the report, make sure you don't have cReport.VerifyDatabase() in place. This often throws up a dialog box asking for the parameter values.
Screenshot of dialog box:
By not calling cReport.VerifyDatabase() this will stop the dialog box from showing.
Now through Crystal Reports you can set the Graphic Location using a parameter. To do this, add the parameter to the report and suppress the field (as you don't want to see it). Then you can add the Report Field as the location.
Add parameter to report and suppress:
Add Report Field as the Graphic Location for the OLE Object:~=
Run the report and input the parameter. This will change the image accordingly. See both screenshots:
SC001::
INT4303:
This however does not seem to work through VB. The ReportViewer does not handle the change of images. Instead you are left with the default image. I'm unsure why this is the case and I'm guessing it's a bug with Crystal. If anyone knows of why this happens, feel free to comment.
EDIT
After discussing the smaller points in chat, we seem to have worked out that the issue arose because of the file path. Saving it to the database as \\server\...\...\image.png was not displaying, but after changing it to Z:\...\...\image.png it has worked fine every time. It was because of the file path, all along.

Move files only if they are image format

i just would like to be sure whether i missed something in my code or not. I want to validate before moving my files from one folder to another that file is image. I prepared this function and usegae like below. Can you please tell me is it ok? Do i need some dispose or anything else or its just quit enought. many thanks, cheers.
Function IsValidImage(filename As String) As Boolean
Try
Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
img.Dispose()
Catch generatedExceptionName As OutOfMemoryException
' Image.FromFile throws an OutOfMemoryException
' if the file does not have a valid image format or
' GDI+ does not support the pixel format of the file.
'
Return False
End Try
Return True
End Function
If IsValidImage("c:\path\to\your\file.ext") Then
'do something
'
Else
'do something else
'
End If
Let the file path be "D:\web\sample\Image\my.Image.png" then you can check whether the file is an image file or not using the following code:
Dim filepath As String = "D:\web\sample\Image\my.Image.png"
Dim imageExtensions() As String = {"bmp", "gif", "jpg", "png", "psd", "psp", "thm", "tif", "yuv"}
Dim pat As String = "\\(?:.+)\\(.+)\.(.+)"
Dim r As Regex = New Regex(pat)
Dim m As Match = r.Match(filepath)
If imageExtensions.Contains(m.Groups(2).Captures(0).ToString()) Then
MsgBox("valid Image")
End If
You are on the right way, but you should make up your mind what you want to whant to gain:
If you want to develop a method telling you wheter the content of a file is considered a .Net recognized picture image, your approach is totally fine.
I start with your (slightly) overworked code:
Function IsValidImage(filename As String) As Boolean
Try
Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
' the file could be opened as image so it is valid
Return True
End Using
Catch notValidException As OutOfMemoryException
' Image.FromFile throws an OutOfMemoryException
' if the file does not have a valid image format or
' GDI+ does not support the pixel format of the file.
'
Return False
End Try
' Every other Exception is considered not to be able too look whether the
' file is an Image or not, so it should be thrown to outside
End Function
Don't misjudge this approach as using Exception for control flow, you are only reacting to the one thrown if the content of the file is not usable for creating an Image. This let's the decision on the framework whether the file is valid for an Imageor not.
Let's refine your Method a little bit, so that you can determine wheter the image in the file does not exceed a given size:
Function IsValidImage(filename As String, maxSize As Size) As Boolean
Try
Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
' Returns True if the image is smaller or equal than maxSize
Return img.Size.Width <= maxSize.Width AndAlso
img.Size.Height <= maxSize.Height
End Using
Catch notValidException As OutOfMemoryException
Return False
End Try
End Function
Also this is not a misuse of Exceptions. It is a practice of fail fast: if I can't get an Image the file is not an Image. Otherwise I check the dimension on the opened Image, making my outcome dependent on operations done with it.
Misusing an Exception for control flow could, but must not be the following:
Sub CheckImage(filename As String)
Try
Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
End Using
Catch notValidException As OutOfMemoryException
Throw New FileIsNoImageException()
End Try
End Sub
Try
CheckImage("c:\path\to\your\file.ext")
'do something
'
Catch invalid As FileIsNoImageException
'do something else
'
End Try
Even this approach has a valid usage if you consider a file not beeing an Image as an error.
But normaly you would do this in functions which give you the Image as return value.
But this is a tottaly no go:
Sub CheckImage(filename As String)
Try
Using img As System.Drawing.Image = System.Drawing.Image.FromFile(filename)
Throw New FileIsImageException() ' DON'T DO SUCH A CRAP
End Using
Catch notValidException As OutOfMemoryException
Throw New FileIsNoImageException()
End Try
End Sub
Try
CheckImage("c:\path\to\your\file.ext")
Catch valid As FileIsImageException
'do something
'
Catch invalid As FileIsNoImageException
'do something else
'
End Try
#Tim, I beg your pardon, but I can't hear this Exception use is a bad practice tale any more.