-
Feb 8th, 2022, 08:23 AM
#1
Thread Starter
Lively Member
resize image dimensions in vb.net
Dear All Master,
I want to resize the dimensions of the image so that it helps the performance of the image that appears in the columnuboudcolumn image in the gridview. Is there any solution?
because if I use directly from the size of the image file it makes it slow in the gridview.
for image file information there are 5000 files with each file having dimensions of 1748x2480
thanks
roy88
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView()
Dim repItemGraphicsEdit As New RepositoryItemPictureEdit()
repItemGraphicsEdit.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
GridControl1.RepositoryItems.Add(repItemGraphicsEdit)
GridView1.Columns("IMAGE1").ColumnEdit = repItemGraphicsEdit
GridView1.Columns("IMAGE2").ColumnEdit = repItemGraphicsEdit
GridView1.Columns("IMAGE3").ColumnEdit = repItemGraphicsEdit
End Sub
Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim view As GridView = TryCast(sender, GridView)
Dim filename1 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1")) Then
filename1 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1"))
Else
filename1 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename2 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2")) Then
filename2 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2"))
Else
filename2 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename3 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3")) Then
filename3 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3"))
Else
filename3 = String.Empty 'or Nothing depending what comes next.
End If
Dim SUBFOLDERP As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP")) Then
SUBFOLDERP = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP"))
Else
SUBFOLDERP = String.Empty 'or Nothing depending what comes next.
End If
Dim img As Image = Nothing
Try
If e.Column.Caption = "IMAGE1" Then
Dim filePath1 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename1, False)
e.Value = GetImage(filePath1)
ElseIf e.Column.Caption = "IMAGE2" Then
Dim filePath2 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename2, False)
e.Value = GetImage(filePath2)
ElseIf e.Column.Caption = "IMAGE3" Then
Dim filePath3 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename3, False)
e.Value = GetImage(filePath3)
Catch
End Try
End Sub
Function GetImage(str As String) As Image
If (Images.ContainsKey(str)) Then
Return CType(Images(str), Image)
Else
Dim img = Image.FromFile(str)
Images.Add(str, img)
Return img
End If
End Function
-
Feb 8th, 2022, 09:00 AM
#2
Re: resize image dimensions in vb.net
What do you not understand about the information you found when you searched the web? If you haven't searched, why not? If you have, what did you find, what did you do and what happened when you did it?
-
Feb 8th, 2022, 09:21 AM
#3
Thread Starter
Lively Member
Re: resize image dimensions in vb.net
Originally Posted by jmcilhinney
What do you not understand about the information you found when you searched the web? If you haven't searched, why not? If you have, what did you find, what did you do and what happened when you did it?
@jmcilhinney,
thank you for your reply, that's why I don't know the solution so I need a solution in this forum.
I'm just a new person and I'm trying to learn and I'm sure in this forum many masters who provide solutions to this problem. I know it might be easy for you but for me it's hard.
Last edited by roy88; Feb 8th, 2022 at 09:27 AM.
-
Feb 8th, 2022, 11:25 AM
#4
Re: resize image dimensions in vb.net
Are you saying that the process is slowed down because of the size of the images? I'm not sure that there's a reasonable solution in your program. I'm not saying that there isn't a reasonable solution, just that it isn't going to be in your program.
What do these images look like? If you were to show a single image of the size that you described, it would look pretty good if it took up the entire screen. It would look pretty bad if it was stuffed into 64x64 pixels. Some images would look just fine stuffed into 64x64, but if that's the case, then they shouldn't be 1748x2480 to begin with. Of course, I'm taking that 64x64 at random, but the general point is correct: Images of that size will look fine at full screen, but will probably look really bad if shrunk to the point where you can get a few of them on the screen at the same time....and there is no WAY you'll get anywhere close to 5000 on the screen at one time.
So, what's the ultimate objective? Even if you wanted to show thumbnails of the images, you can't get 5000 on the screen. At most, you can get only a few on the screen at any one time. That makes me think that the slowdown isn't coming from what you may think it is coming from. Only a few need to be viewed at any time, so only a few need to be dealt with at any time. It may be that the control has decided to process all 5000 at once, and you may not be able to do much about that, DevExpress can be a bit weird about things like that, at times. Still, I think you need to figure out what the actual problem is.
I would start by doing a couple things:
1) How many images do you realistically expect to see on the screen at any time? Given that number, reduce your set of images to just that number and see how the performance is. If it is fine, then the issue may be that all the non-displayed images are getting processed at once. There may well be better ways to do that.
2) Put at least SOMETHING into those Catch blocks (or get rid of those Try..Catch blocks entirely). If you are getting exceptions, they will slow your program to a crawl, even with you ignoring them. Not knowing about the exceptions could have a terrible cost. I had a program that worked fine for a few records, but performance began to degrade very fast as the number of records increased. After a bit of looking, I realized that an inner function (which I didn't write) used exception handling to deal with unexceptional situations that could have been dealt with using a simple If. Doing that a few times wasn't noticeable, but doing it a hundred times took noticeable time, and then things went from bad to worse.
My usual boring signature: Nothing
-
Feb 8th, 2022, 07:32 PM
#5
Re: resize image dimensions in vb.net
Originally Posted by roy88
@jmcilhinney,
thank you for your reply, that's why I don't know the solution so I need a solution in this forum.
I'm just a new person and I'm trying to learn and I'm sure in this forum many masters who provide solutions to this problem. I know it might be easy for you but for me it's hard.
The problem is that you're making no effort on your own behalf and expecting someone else to write the code for you. It's not about how new you are or how easy it might be. It's about doing what you can for yourself first and then asking for help with the bits you can't. You've asked for help with everything without doing anything for yourself first. You may be new to programming but you're not new to the internet or search engines so what's stopping you typing "vb.net resize image" into a search engine and then using the results as best you can? If you find all the information you need then there's no need to ask for help. If you still have an issue then you can ask about that specific issue rather than the whole thing. I'm not telling anyone not to use this forum or other sites like it. I'm telling them to make what effort they can first and then use such sites for the parts they can't work out for themselves. If you haven't searched the web first then you haven't really tried.
-
Feb 8th, 2022, 08:53 PM
#6
Thread Starter
Lively Member
Re: resize image dimensions in vb.net
Originally Posted by Shaggy Hiker
Are you saying that the process is slowed down because of the size of the images? I'm not sure that there's a reasonable solution in your program. I'm not saying that there isn't a reasonable solution, just that it isn't going to be in your program.
What do these images look like? If you were to show a single image of the size that you described, it would look pretty good if it took up the entire screen. It would look pretty bad if it was stuffed into 64x64 pixels. Some images would look just fine stuffed into 64x64, but if that's the case, then they shouldn't be 1748x2480 to begin with. Of course, I'm taking that 64x64 at random, but the general point is correct: Images of that size will look fine at full screen, but will probably look really bad if shrunk to the point where you can get a few of them on the screen at the same time....and there is no WAY you'll get anywhere close to 5000 on the screen at one time.
So, what's the ultimate objective? Even if you wanted to show thumbnails of the images, you can't get 5000 on the screen. At most, you can get only a few on the screen at any one time. That makes me think that the slowdown isn't coming from what you may think it is coming from. Only a few need to be viewed at any time, so only a few need to be dealt with at any time. It may be that the control has decided to process all 5000 at once, and you may not be able to do much about that, DevExpress can be a bit weird about things like that, at times. Still, I think you need to figure out what the actual problem is.
I would start by doing a couple things:
1) How many images do you realistically expect to see on the screen at any time? Given that number, reduce your set of images to just that number and see how the performance is. If it is fine, then the issue may be that all the non-displayed images are getting processed at once. There may well be better ways to do that.
2) Put at least SOMETHING into those Catch blocks (or get rid of those Try..Catch blocks entirely). If you are getting exceptions, they will slow your program to a crawl, even with you ignoring them. Not knowing about the exceptions could have a terrible cost. I had a program that worked fine for a few records, but performance began to degrade very fast as the number of records increased. After a bit of looking, I realized that an inner function (which I didn't write) used exception handling to deal with unexceptional situations that could have been dealt with using a simple If. Doing that a few times wasn't noticeable, but doing it a hundred times took noticeable time, and then things went from bad to worse.
@Shaggy Hiker
Thanks for your reply. As you said it looks bad or looks small with a size of 64x64 pixels, then it doesn't matter because later the original image is displayed in the picturebox. So
It does not matter because it looks small or the pixel decreases in the gridview because later by clicking the cell image then the original image appears in the picture box.
Thanks
roy88
-
Feb 8th, 2022, 09:53 PM
#7
Re: resize image dimensions in vb.net
So, did you try the couple of things that I suggested? Did you find out anything from that?
My usual boring signature: Nothing
-
Feb 8th, 2022, 10:20 PM
#8
Thread Starter
Lively Member
Re: resize image dimensions in vb.net
Originally Posted by Shaggy Hiker
So, did you try the couple of things that I suggested? Did you find out anything from that?
@ShaggyHiker
I used the image resize function but it hasn't worked
Thanks
roy88
Code:
Public Shared Function ResizeImage(ByVal InputImage As Image) As Image
Return New Bitmap(InputImage, New Size(64, 64))
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView()
Dim repItemGraphicsEdit As New RepositoryItemPictureEdit()
repItemGraphicsEdit.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
GridControl1.RepositoryItems.Add(repItemGraphicsEdit)
GridView1.Columns("IMAGE1").ColumnEdit = repItemGraphicsEdit
GridView1.Columns("IMAGE2").ColumnEdit = repItemGraphicsEdit
GridView1.Columns("IMAGE3").ColumnEdit = repItemGraphicsEdit
End Sub
Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim view As GridView = TryCast(sender, GridView)
Dim filename1 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1")) Then
filename1 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1"))
Else
filename1 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename2 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2")) Then
filename2 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2"))
Else
filename2 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename3 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3")) Then
filename3 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3"))
Else
filename3 = String.Empty 'or Nothing depending what comes next.
End If
Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim view As GridView = TryCast(sender, GridView)
Dim filename1 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1")) Then
filename1 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1"))
Else
filename1 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename2 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2")) Then
filename2 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2"))
Else
filename2 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename3 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3")) Then
filename3 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3"))
Else
filename3 = String.Empty 'or Nothing depending what comes next.
End If
Dim SUBFOLDERP As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP")) Then
SUBFOLDERP = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP"))
Else
SUBFOLDERP = String.Empty 'or Nothing depending what comes next.
End If
Dim img As Image = Nothing
Try
If e.Column.Caption = "IMAGE1" Then
Dim filePath1 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename1, False)
Dim source1 As New Bitmap(filePath1)
source1 = ResizeImage(source1)
e.Value = GetImage(filePath1)
ElseIf e.Column.Caption = "IMAGE2" Then
Dim filePath2 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename2, False)
Dim source2 As New Bitmap(filePath2)
source2 = ResizeImage(source2)
e.Value = GetImage(filePath2)
ElseIf e.Column.Caption = "IMAGE3" Then
Dim filePath3 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename3, False)
Dim source3 As New Bitmap(filePath3)
source3 = ResizeImage(source3)
e.Value = GetImage(filePath3)
End If
Catch
End Try
End Sub
Function GetImage(str As String) As Image
If (Images.ContainsKey(str)) Then
Return CType(Images(str), Image)
Else
Dim img = Image.FromFile(str)
Images.Add(str, img)
Return img
End If
End Function
-
Feb 8th, 2022, 10:32 PM
#9
Re: resize image dimensions in vb.net
vb.net Code:
Dim source1 As New Bitmap(filePath1)
source1 = ResizeImage(source1)
e.Value = GetImage(filePath1)
What exactly do you think that code is doing?
-
Feb 9th, 2022, 12:21 AM
#10
Thread Starter
Lively Member
Re: resize image dimensions in vb.net
Originally Posted by jmcilhinney
vb.net Code:
Dim source1 As New Bitmap(filePath1)
source1 = ResizeImage(source1)
e.Value = GetImage(filePath1)
What exactly do you think that code is doing?
Code:
Public Shared Function ResizeImage(ByVal InputImage As Image) As Image
Return New Bitmap(InputImage, New Size(64, 64))
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView()
Dim repItemGraphicsEdit As New RepositoryItemPictureEdit()
repItemGraphicsEdit.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
GridControl1.RepositoryItems.Add(repItemGraphicsEdit)
GridView1.Columns("IMAGE1").ColumnEdit = repItemGraphicsEdit
GridView1.Columns("IMAGE2").ColumnEdit = repItemGraphicsEdit
GridView1.Columns("IMAGE3").ColumnEdit = repItemGraphicsEdit
End Sub
Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim view As GridView = TryCast(sender, GridView)
Dim filename1 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1")) Then
filename1 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1"))
Else
filename1 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename2 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2")) Then
filename2 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2"))
Else
filename2 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename3 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3")) Then
filename3 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3"))
Else
filename3 = String.Empty 'or Nothing depending what comes next.
End If
Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim view As GridView = TryCast(sender, GridView)
Dim filename1 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1")) Then
filename1 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1"))
Else
filename1 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename2 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2")) Then
filename2 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2"))
Else
filename2 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename3 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3")) Then
filename3 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3"))
Else
filename3 = String.Empty 'or Nothing depending what comes next.
End If
Dim SUBFOLDERP As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP")) Then
SUBFOLDERP = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP"))
Else
SUBFOLDERP = String.Empty 'or Nothing depending what comes next.
End If
Dim img As Image = Nothing
Try
If e.Column.Caption = "IMAGE1" Then
Dim filePath1 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename1, False)
e.Value = ResizeImage(GetImage(filePath1))
ElseIf e.Column.Caption = "IMAGE2" Then
Dim filePath2 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename2, False)
e.Value = ResizeImage(GetImage(filePath2))
ElseIf e.Column.Caption = "IMAGE3" Then
Dim filePath3 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename3, False)
e.Value = ResizeImage(GetImage(filePath3))
End If
Catch
End Try
End Sub
Function GetImage(str As String) As Image
If (Images.ContainsKey(str)) Then
Return CType(Images(str), Image)
Else
Dim img = Image.FromFile(str)
Images.Add(str, img)
Return img
End If
End Function
-
Feb 9th, 2022, 09:43 AM
#11
Thread Starter
Lively Member
Re: resize image dimensions in vb.net
Dear All Master,
I only use one column image, namely "IMAGE1" and I've also resized the image but it's still slow, and I attached one image so it's easy to analyze.
Thanks
https://drive.google.com/file/d/1yGn...ew?usp=sharing
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView()
End Sub
Private Sub gridView1_CustomUnboundColumnData(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs) Handles GridView1.CustomUnboundColumnData
Dim view As GridView = TryCast(sender, GridView)
Dim filename1 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1")) Then
filename1 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME1"))
Else
filename1 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename2 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2")) Then
filename2 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME2"))
Else
filename2 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename3 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3")) Then
filename3 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME3"))
Else
filename3 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename4 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME4")) Then
filename4 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME4"))
Else
filename4 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename5 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME5")) Then
filename5 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME5"))
Else
filename5 = String.Empty 'or Nothing depending what comes next.
End If
Dim filename6 As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME6")) Then
filename6 = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "FILENAME6"))
Else
filename6 = String.Empty 'or Nothing depending what comes next.
End If
Dim SUBFOLDERP As String
If Not DBNull.Value.Equals(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP")) Then
SUBFOLDERP = CStr(view.GetListSourceRowCellValue(e.ListSourceRowIndex, "SUBFOLDERP"))
Else
SUBFOLDERP = String.Empty 'or Nothing depending what comes next.
End If
Dim img As Image = Nothing
Try
If e.Column.Caption = "IMAGE1" Then
Dim filePath1 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & SUBFOLDERP, filename1, False)
e.Value = resizeimage(GetImage(filePath1), 16, 16)
End If
Catch
End Try
End Sub
Function GetImage(str As String) As Image
If (Images.ContainsKey(str)) Then
Return CType(Images(str), Image)
Else
Dim img = Image.FromFile(str)
Images.Add(str, img)
Return img
End If
End Function
Private Function resizeimage(ByVal img As Image, ByVal w As Integer, ByVal h As Integer)
Try
Dim newImageSize As New bitmap(w, h)
Using g As Graphics = Graphics.FromImage(newImageSize)
g.interpolationMode = Drawing2D.interpolationMode.HighQualityBicubic
g.DrawImage(img, New Rectangle(0, 0, w, h))
End Using
Return newImageSize
Catch Ex As Exception
MsgBox(Ex.ToString)
Return Nothing
End Try
Return Nothing
End Function
-
Feb 9th, 2022, 10:52 AM
#12
Re: resize image dimensions in vb.net
Slow, sure, but does it work? If it works, you have the beginnings of a solution there.
I assume that Images is a dictionary (of string, Object), or perhaps (of String, Image). If it is the latter, than there is no point in doing that CType in GetImage, and there isn't much point in that conditional anyways, as there's a TryGetValue member of dictionaries that would be a bit better.
Still, those are minor points. If you have an image dictionary, why not have a second image dictionary for the small images? When you add an image to the Images dictionary, add the reduced size image to the second dictionary. As it stands, you have to compress every image every time you visit it. If that's a slow step, then only do it once and hold onto the reduced image so that you don't have to compress it every time.
From there you could go further, because you might be able to build both dictionaries in a background thread such that they take no time at all for the user. Whether or not that is really possible depends on whether or not there is something useful to do while the background thread is running, which there may not be. Still, don't be compressing the image every single time you visit it.
My usual boring signature: Nothing
-
Feb 9th, 2022, 09:10 PM
#13
Thread Starter
Lively Member
Re: resize image dimensions in vb.net
Originally Posted by Shaggy Hiker
Slow, sure, but does it work? If it works, you have the beginnings of a solution there.
I assume that Images is a dictionary (of string, Object), or perhaps (of String, Image). If it is the latter, than there is no point in doing that CType in GetImage, and there isn't much point in that conditional anyways, as there's a TryGetValue member of dictionaries that would be a bit better.
Still, those are minor points. If you have an image dictionary, why not have a second image dictionary for the small images? When you add an image to the Images dictionary, add the reduced size image to the second dictionary. As it stands, you have to compress every image every time you visit it. If that's a slow step, then only do it once and hold onto the reduced image so that you don't have to compress it every time.
From there you could go further, because you might be able to build both dictionaries in a background thread such that they take no time at all for the user. Whether or not that is really possible depends on whether or not there is something useful to do while the background thread is running, which there may not be. Still, don't be compressing the image every single time you visit it.
@ShaggyHiker
Thanks for your reply.
for the function resize image it is successful but performance is still slow.
So how's the solution?
Thanks
roy88
-
Feb 9th, 2022, 09:24 PM
#14
Re: resize image dimensions in vb.net
Originally Posted by roy88
performance is still slow.
Is it, though? How did you determine that? It takes a long time for a plane to fly from London to New York. Does that mean that the plane is slow? No, it doesn't. The plane is fast but, if it has a long way to go, it will still take a long time to do so. If you have a lot of images to resize then it will still take a long time to resize them. How many images are you resizing? How long does it take to resize one image? How did you determine that there should be a way to do the exact same thing in less time?
-
Feb 9th, 2022, 09:29 PM
#15
Re: resize image dimensions in vb.net
I agree. Time the resizing of just one image. Forget about where it is located for the time being. Start a stopwatch object, pass an image to your ResizeImage method, then stop the Stopwatch object and check the elapsed milliseconds. How long did it take? That's the key value you need to have to figure out what the best approach is.
My usual boring signature: Nothing
-
Feb 12th, 2022, 08:09 AM
#16
Re: resize image dimensions in vb.net
I haven't read all the code in this thread, but I can't help feeling that it's overcomplicated. After all, you can resize an image with one short line of code:
Code:
Dim img As New Bitmap(sourceImage, width, height)
This method uses the default InterpolationMode, which is more than twice as fast as the HighQuality modes. There won't be much visible degradation of the image when reducing size, let alone at thumbnail size.
Another serious cause of slowdown is using Try/Catch. It's meant purely for debugging purposes, or for dealing with unpredictable events outside your own control of the code. And an empty Catch block is a pure waste of time since it merely disguises coding errors.
For shrinking large images, by far the biggest gain in speed will normally come from using Image.GetThumbnailImage. Large images are generally photographic in origin and contain an embedded thumbnail with a typical size (vague memory) of 256*256 pixels, so reducing it to 64*64 would happen in microseconds.
-
Feb 12th, 2022, 09:29 AM
#17
Re: resize image dimensions in vb.net
To be clear: Try...Catch has zero cost so long as no exception is raised. If an exception IS raised, the cost can be VERY high (crashing the program is even higher, though, which is why Try...Catch is there), but there's no cost to having one if nothing gets raised.
My usual boring signature: Nothing
-
Feb 19th, 2022, 06:52 PM
#18
Re: resize image dimensions in vb.net
I think the speed bottleneck that roy88 hasn’t told you about is that he wants to display a filename (not path) in a dgv cell(actually 4 cells per row) which he then wants to databind to 4 thumbnail pictureboxes. To find the image files, he needs to search a folder and all subfolders. This folder structure contains 5000 image files. Then lastly, when the thumbnail images are clicked, a larger version of the image is displayed in another picturebox.
@roy88: Or am i wrong? Looks like your solution has changed, just not sure how much...
Last edited by .paul.; Feb 19th, 2022 at 10:14 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
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
|