-
Apr 11th, 2017, 10:09 AM
#1
Thread Starter
Lively Member
[RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
Hello All,
I am a VB .NET novice and would like to get help in adding banner info onto jpeg images.
I guess the code sequence would be
1) The jpeg images on a computer folder need to be loaded/accessed in VB .NET...then
2) Add banner onto the images later. I would like to have below info on the banner:
> image file name
> current date and time
> Microsoft Window Log-in user name (i.e. JSmith)
> Page numbers. (i.e Page 1 of 6, Page 2 of 6, Page 3 of 6....etc)
The banner info should be at the same position/location on the bottom left-corner of the jpeg images.
3) After adding above banner info, the images are to be saved as JPG (or PDF) file format
I start learning and using VB Studio 2017. However, I believe codes/syntax made in earlier VB Studio versions could work in VB Studio 2017 since my issue above may only use same syntax/coding done in those earlier VB version such as VB Studio 2010, 203, 2015...etc
Any help would be grateful.
Johny
-
Apr 11th, 2017, 05:23 PM
#2
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Start new project and add PictureBox1 and Button1 then paste the following code (it is quick solution, but must be better solution out there)
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As String = "path\to\picture"
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
PictureBox1.Load(f)
Dim cBanner As New BannerData With {.FileName = f, .DateAndTime = Now, .UserName = "JSmith", .PageNumber = 2}
DrawBanner(PictureBox1, cBanner)
PictureBox1.Image.Save(f & "_with_banner.jpg", Imaging.ImageFormat.Jpeg) ' save copy of the picture
End Sub
Private Sub DrawBanner(ByVal picSource As PictureBox, ByVal cBanner As BannerData)
Dim gfx As Graphics = Graphics.FromImage(picSource.Image)
Dim sb As New Text.StringBuilder
sb.AppendLine(cBanner.FileName)
sb.AppendLine(cBanner.DateAndTime.ToString)
sb.AppendLine(cBanner.UserName)
sb.AppendLine(cBanner.PageNumber.ToString)
' Draw banner shadow first
gfx.DrawString(sb.ToString, New Font("Arial", 8, FontStyle.Regular), Brushes.White, New Point(9, picSource.Height - 98))
' Draw banner
gfx.DrawString(sb.ToString, New Font("Arial", 8, FontStyle.Regular), Brushes.Black, New Point(8, picSource.Height - 100)) ' Subtract reasonable height to show all lines.
End Sub
End Class
Public Class BannerData
Public FileName As String
Public DateAndTime As Date
Public UserName As String
Public PageNumber As Integer
End Class
Last edited by 4x2y; Apr 11th, 2017 at 06:39 PM.
-
Apr 11th, 2017, 09:08 PM
#3
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Thank you 4x2y for your quick reply and help.
I wonder if I can specify the location on the image where the banner starts. As mentioned in my first post, I want to have the banner at the bottom left corner of the picture and the text should be vertical and parallel with the edge of the paper.
How could I code that with x, y position ?
Thank you again
-
Apr 11th, 2017, 09:47 PM
#4
Re: Need help in how to add banner information onto JPEG (JPG) images ???
That's what the New Point does in the DrawString... it sets the start X & Y of the text it is about to draw... does it not do that?
-tg
-
Apr 11th, 2017, 10:04 PM
#5
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Yes, I see the DrawString function....
I will start testing your code today and let you know what the result would be.
If I have more questions and good result I will let you know promptly.
Thanks again
-
Apr 11th, 2017, 10:16 PM
#6
Re: Need help in how to add banner information onto JPEG (JPG) images ???
You didn't say you want vertical text!!
Replace
Code:
' Draw banner shadow first
gfx.DrawString(sb.ToString, New Font("Arial", 8, FontStyle.Regular), Brushes.White, New Point(9, picSource.Height - 98))
' Draw banner
gfx.DrawString(sb.ToString, New Font("Arial", 8, FontStyle.Regular), Brushes.Black, New Point(8, picSource.Height - 100)) ' Subtract reasonable height to show all lines.
with
Code:
' Draw banner shadow first
gfx.DrawString(sb.ToString, New Font("Arial", 8, FontStyle.Regular), Brushes.White, New Point(201, picSource.Height - 501), New StringFormat With {.FormatFlags = StringFormatFlags.DirectionVertical Or StringFormatFlags.DirectionRightToLeft})
' Draw banner
gfx.DrawString(sb.ToString, New Font("Arial", 8, FontStyle.Regular), Brushes.Black, New Point(200, picSource.Height - 500), New StringFormat With {.FormatFlags = StringFormatFlags.DirectionVertical Or StringFormatFlags.DirectionRightToLeft}) ' Subtract reasonable height to show all lines.
-
Apr 12th, 2017, 06:19 AM
#7
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Thank you again.....
Even I do not understand all of your code yet but it looks complex. I need to digest your code, test it and will definitely let you know the result. I confidently your code works fine. If there is any error or issue, I will let you know promptly.
Thanks 4x2y
-
Apr 12th, 2017, 07:19 AM
#8
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Hi 4x2y,
In your code line "Dim f As String = "path\to\picture", I need to put down the exact file location and name such as
D:\TEST\CATIAVB.JPG in double-quotation mark?
Can I use a string variable (Dim pathString As String = U:\TEST\CATIAVB.JPG) in place of whole "U:\TEST\CATIAVB.JPG" text?
Thanks again
-
Apr 12th, 2017, 10:19 AM
#9
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Yes. In fact, it's probably the far more common way to do this. A variable that holds a string is just as good as a string literal, and either can be used.
My usual boring signature: Nothing
-
Apr 12th, 2017, 09:33 PM
#10
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by Shaggy Hiker
Yes. In fact, it's probably the far more common way to do this. A variable that holds a string is just as good as a string literal, and either can be used.
Hi 4x2y,
I tested your code and it actually works. Thank you again. However,
1) The text written is near the center of the page. I want it to be near the bottom left corner of the image/page.
I just change the margin number in the program?
2) How could I get "actual computer log-in username" and feed it to the code?
3) For page numbers, can I use a variable and get page numbers change automatically and print on the images/papers such as
Page 1 of 6, Page 2 of 6.....etc?
Thank you very much
Johny
-
Apr 13th, 2017, 06:06 AM
#11
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Hi 4x2y,
I wonder if your code can be adapted in VB 6? If yes, is it difficult to transfer/adapt?
Thanks again
-
Apr 13th, 2017, 06:32 AM
#12
Re: Need help in how to add banner information onto JPEG (JPG) images ???
If you wanted a VB6 solution 1)You should have posted in the VB6 section and 2) You should have said that right from the start. A VB6 solution would require a completelhy different set of code instructions.
-tg
-
Apr 13th, 2017, 08:30 AM
#13
Re: Need help in how to add banner information onto JPEG (JPG) images ???
I am quite surprised that it has not been noted ... but I would NOT recommend loading you images into a picture box (as 4x2y suggested) just to print some text on them and save them back to a file...
I am not going to spend time specifying a solution though since you apparently want this for vb6 (that may not work as i don't have anything to test on) and I have (happily) not touched on that for years!
Kris
EDIT:
Why do you state this in your opening post:
Originally Posted by NeedHelpVB
I start learning and using VB Studio 2017. However, I believe codes/syntax made in earlier VB Studio versions could work in VB Studio 2017 since my issue above may only use same syntax/coding done in those earlier VB version such as VB Studio 2010, 203, 2015...etc
Just to state this later:
Originally Posted by NeedHelpVB
I wonder if your code can be adapted in VB 6? If yes, is it difficult to transfer/adapt?
Last edited by i00; Apr 13th, 2017 at 08:33 AM.
-
Apr 13th, 2017, 11:39 AM
#14
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by i00
I am quite surprised that it has not been noted ... but I would NOT recommend loading you images into a picture box (as 4x2y suggested) just to print some text on them and save them back to a file...
I am not going to spend time specifying a solution though since you apparently want this for vb6 (that may not work as i don't have anything to test on) and I have (happily) not touched on that for years!
Kris
EDIT:
Why do you state this in your opening post:
Just to state this later:
====================
Sorry if I confuse you.
I really want to learn VS .NET since VB 6 is too old and not suitable for future development.
When I ask if the code can be adapted to VB 6 since I use a CAD application and it has VB 6 integrated in it. However, I do not know how to use VB6 and do not want to use VB 6 for above reason. Hope this clear your thought.
Wonder if you are still able to help me above issues I copied here:
1) The text written is near the center of the page. I want it to be near the bottom left corner of the image/page.
I just change the margin number in the program?
2) How could I get "actual computer log-in username" and feed it to the code?
3) For page numbers, can I use a variable and get page numbers change automatically and print on the images/papers such as Page 1 of 6, Page 2 of 6.....etc?
I still thank you for your help
Johny
-
Apr 13th, 2017, 02:30 PM
#15
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by i00
I am quite surprised that it has not been noted ... but I would NOT recommend loading you images into a picture box (as 4x2y suggested) just to print some text on them and save them back to a file...
I have noted as i wrote "(it is quick solution, but must be better solution out there)"
-
Apr 13th, 2017, 02:50 PM
#16
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
If not loading images into Picture Box, then how could I write text on the images?
Your guide/help is much appreciated.
-
Apr 13th, 2017, 04:30 PM
#17
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by NeedHelpVB
1) The text written is near the center of the page. I want it to be near the bottom left corner of the image/page.
Try this new method which auto measure text height and width
Code:
Private Sub DrawBanner(ByVal picSource As PictureBox, ByVal cBanner As BannerData)
Dim gfx As Graphics = Graphics.FromImage(picSource.Image)
Dim sb As New Text.StringBuilder
sb.AppendLine(cBanner.FileName)
sb.AppendLine(cBanner.DateAndTime.ToString)
sb.AppendLine(cBanner.UserName)
sb.AppendLine(cBanner.PageNumber.ToString)
Dim cFont As Font = New Font("Arial", 8, FontStyle.Regular)
Dim cSizeF As SizeF = gfx.MeasureString(sb.ToString, cFont)
' Vertical
Dim cFormat As StringFormat = New StringFormat With {.FormatFlags = StringFormatFlags.DirectionVertical Or StringFormatFlags.DirectionRightToLeft}
' Draw banner shadow first
gfx.DrawString(sb.ToString, cFont, Brushes.White, cSizeF.Height + 1, picSource.Height - cSizeF.Width - 1, cFormat)
' Draw banner
gfx.DrawString(sb.ToString, cFont, Brushes.Black, cSizeF.Height, picSource.Height - cSizeF.Width, cFormat)
''Uncomment the following to draw horizontal text
'' Draw banner shadow first
'gfx.DrawString(sb.ToString, cFont, Brushes.White, 16, picSource.Height - cSizeF.Height - 1)
'' Draw banner
'gfx.DrawString(sb.ToString, cFont, Brushes.Black, 15, picSource.Height - cSizeF.Height)
End Sub
Originally Posted by NeedHelpVB
2) How could I get "actual computer log-in username" and feed it to the code?
Code:
Environment.UserName
Originally Posted by NeedHelpVB
3) For page numbers, can I use a variable and get page numbers change automatically and print on the images/papers such as
Page 1 of 6, Page 2 of 6.....etc?
Yes you can, you can build like this
Code:
"Page " & variable_name.ToString & " of 6"
-
Apr 13th, 2017, 09:36 PM
#18
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by 4x2y
Try this new method which auto measure text height and width
Code:
Private Sub DrawBanner(ByVal picSource As PictureBox, ByVal cBanner As BannerData)
Dim gfx As Graphics = Graphics.FromImage(picSource.Image)
Dim sb As New Text.StringBuilder
sb.AppendLine(cBanner.FileName)
sb.AppendLine(cBanner.DateAndTime.ToString)
sb.AppendLine(cBanner.UserName)
sb.AppendLine(cBanner.PageNumber.ToString)
Dim cFont As Font = New Font("Arial", 8, FontStyle.Regular)
Dim cSizeF As SizeF = gfx.MeasureString(sb.ToString, cFont)
' Vertical
Dim cFormat As StringFormat = New StringFormat With {.FormatFlags = StringFormatFlags.DirectionVertical Or StringFormatFlags.DirectionRightToLeft}
' Draw banner shadow first
gfx.DrawString(sb.ToString, cFont, Brushes.White, cSizeF.Height + 1, picSource.Height - cSizeF.Width - 1, cFormat)
' Draw banner
gfx.DrawString(sb.ToString, cFont, Brushes.Black, cSizeF.Height, picSource.Height - cSizeF.Width, cFormat)
''Uncomment the following to draw horizontal text
'' Draw banner shadow first
'gfx.DrawString(sb.ToString, cFont, Brushes.White, 16, picSource.Height - cSizeF.Height - 1)
'' Draw banner
'gfx.DrawString(sb.ToString, cFont, Brushes.Black, 15, picSource.Height - cSizeF.Height)
End Sub
Code:
Environment.UserName
Yes you can, you can build like this
Code:
"Page " & variable_name.ToString & " of 6"
---------------------------
Hi 4x2y,
Your new code works better. I used environment.username in your given name JSmith and it worked perfectly.
I have a few jpg images and start trying to put them in a loop, put those banner data on, and print images out one-by-one as jpg as well.
Any tips for me?
Thank you for your help
-
Apr 13th, 2017, 10:45 PM
#19
Re: Need help in how to add banner information onto JPEG (JPG) images ???
If your images are in folder, try this
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
Dim cBanner As BannerData
Dim t As Date = Now ' store date, so it is not change inside the loop
Dim strFiles() As String = IO.Directory.GetFiles("replace_with_your_folder_path", "*.jpg", IO.SearchOption.TopDirectoryOnly)
Me.Cursor = Cursors.WaitCursor
For j = 0 To strFiles.Length - 1
PictureBox1.Load(strFiles(j))
cBanner = New BannerData
cBanner.FileName = strFiles(j)
cBanner.DateAndTime = t
cBanner.UserName = Environment.UserName
cBanner.PageNumber = "Page " & j + 1 & " of " & strFiles.Length
DrawBanner(PictureBox1, cBanner)
PictureBox1.Image.Save(strFiles(j) & "_with_banner.jpg", Imaging.ImageFormat.Jpeg) ' save copy of the picture
Next
Me.Cursor = Cursors.Default
End Sub
Important: change declaration of PageNumber in BannerData class from Integer to String.
-
Apr 15th, 2017, 10:58 AM
#20
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by 4x2y
If your images are in folder, try this
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
Dim cBanner As BannerData
Dim t As Date = Now ' store date, so it is not change inside the loop
Dim strFiles() As String = IO.Directory.GetFiles("replace_with_your_folder_path", "*.jpg", IO.SearchOption.TopDirectoryOnly)
Me.Cursor = Cursors.WaitCursor
For j = 0 To strFiles.Length - 1
PictureBox1.Load(strFiles(j))
cBanner = New BannerData
cBanner.FileName = strFiles(j)
cBanner.DateAndTime = t
cBanner.UserName = Environment.UserName
cBanner.PageNumber = "Page " & j + 1 & " of " & strFiles.Length
DrawBanner(PictureBox1, cBanner)
PictureBox1.Image.Save(strFiles(j) & "_with_banner.jpg", Imaging.ImageFormat.Jpeg) ' save copy of the picture
Next
Me.Cursor = Cursors.Default
End Sub
Important: change declaration of PageNumber in BannerData class from Integer to String.
------------------
1) You said "change declaration of PageNumber in BannerData class from Integer to String." Is it in the sub "DrawBanner"?
I see the line in that sub: sb.AppendLine(cBanner.PageNumber.ToString). Is PageNumber already converted to String?
2) I do not see the code line(s) that print banner text vertically in the Sub Button1_Click.
Does it mean that I need to link two sub's: Sub Button1_Click and Sub DrawBanner together to have all jpeg files (in the same folder) with printed text vertically?
3) What do below code lines do?
Me.Cursor = Cursors.WaitCursor
Me.Cursor = Cursors.Default
Thank you again
-
Apr 15th, 2017, 03:33 PM
#21
Re: Need help in how to add banner information onto JPEG (JPG) images ???
1) You said "change declaration of PageNumber in BannerData class from Integer to String." Is it in the sub "DrawBanner"?
Code:
Public Class BannerData
Public FileName As String
Public DateAndTime As Date
Public UserName As String
Public PageNumber As String ' I mean this one.
End Class
2) I do not see the code line(s) that print banner text vertically in the Sub Button1_Click.
Does it mean that I need to link two sub's: Sub Button1_Click and Sub DrawBanner together to have all jpeg files (in the same folder) with printed text vertically?
Yes, all other parts, just replace previous Sub Button1_Click with the new one.
3) What do below code lines do?
Me.Cursor = Cursors.WaitCursor
Me.Cursor = Cursors.Default
Because the new code contains loop that open many files and may takes long time to complete, i added Me.Cursor = Cursors.WaitCursor before the loop to change mouse cursor to hourglass then Me.Cursor = Cursors.Default after the loop to back mouse cursor to normal. This is useful specially if someone else will use your program, so he don't think the program is frozen if the loop take long time to complete.
-
Apr 15th, 2017, 10:58 PM
#22
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Hi 4x2y,
Your modified works great. Thank you
1) If I want page # right after user name (to reduce number of text line), how can I do? such as John, Page 1 of 6.
I tried
cBanner.UserName = "Printed by " & Environment.UserName & cBanner.PageNumber = "Page " & j + 1 & " of " & strFiles.Length
but it did not work.
2) Me. statement. What does it exactly do? I read some info online but I am still confused.
3) Is it possible to make an .exe file so that it can be run on different computers? If yes, how could I do that? Do other computers need to have Visual Studio/Express installed in order to run the .exe file?
Please kindly advise. Thank you again
-
Apr 16th, 2017, 04:06 PM
#23
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Let's refine the code, here is a better one which encapsulate most work in Banner class and not using PictureBox anymore
Don't mix the new code with previous, instead start new project and add Button1 then paste the following code
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cImage As Image
Dim cBanner As New Banner
Dim t As Date = Now ' store date, so it is not change inside the loop
Dim strFiles() As String = IO.Directory.GetFiles("replace_with_your_folder_path", "*.jpg", IO.SearchOption.TopDirectoryOnly)
cBanner.TextDirection = Banner.Direction.Vertical
cBanner.Font = New Font("Courier New", 8, FontStyle.Regular)
cBanner.Color = Brushes.Red
cBanner.ShadowColor = Brushes.Yellow
Me.Cursor = Cursors.WaitCursor
For j = 0 To strFiles.Length - 1
cImage = Image.FromFile(strFiles(j))
cBanner.Text = strFiles(j) & ControlChars.NewLine &
t.ToString & ControlChars.NewLine &
"Printed by " & Environment.UserName & " \ Page " & j + 1 & " Of " & strFiles.Length
cBanner.Draw(cImage)
cImage.Save(strFiles(j) & "_with_banner.jpg", Imaging.ImageFormat.Jpeg) ' save copy of the picture
Next
Me.Cursor = Cursors.Default
End Sub
End Class
Public Class Banner
Public Enum Direction
Vertical
Horizontal
End Enum
Public Text As String
Public Font As Font
Public TextDirection As Direction
Public Color As Brush
Public ShadowColor As Brush
Public Sub New()
' Set default values
Text = "Banner"
TextDirection = Direction.Vertical
Font = New Font("Arial", 8, FontStyle.Regular)
Color = Brushes.White
ShadowColor = Brushes.Black
End Sub
Public Sub Draw(ByVal cTargetImage As Image)
Dim gfx As Graphics = Graphics.FromImage(cTargetImage)
Dim cSizeF As SizeF = gfx.MeasureString(Me.Text, Me.Font)
Select Case Me.TextDirection
Case Direction.Vertical
Dim cFormat As StringFormat = New StringFormat With {.FormatFlags = StringFormatFlags.DirectionVertical Or StringFormatFlags.DirectionRightToLeft}
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, cSizeF.Height + 1, cTargetImage.Height - cSizeF.Width - 1, cFormat)
gfx.DrawString(Me.Text, Me.Font, Me.Color, cSizeF.Height, cTargetImage.Height - cSizeF.Width, cFormat)
Case Direction.Horizontal
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, 16, cTargetImage.Height - cSizeF.Height - 1)
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, 15, cTargetImage.Height - cSizeF.Height)
End Select
End Sub
End Class
Edit:
I have moved the following lines out of For...Next loop as they aren't change.
Code:
cBanner.TextDirection = Banner.Direction.Vertical
cBanner.Font = New Font("Courier New", 8, FontStyle.Regular)
cBanner.Color = Brushes.Red
cBanner.ShadowColor = Brushes.Yellow
3) Is it possible to make an .exe file so that it can be run on different computers? If yes, how could I do that?
Yes, after finishing go to Build menu and select Publish
Do other computers need to have Visual Studio/Express installed in order to run the .exe file?
No, only your project's target framework must be installed on that computer, to know target framework, go to Project menu and select Properties... and see Application page
Last edited by 4x2y; Apr 16th, 2017 at 04:44 PM.
Reason: Optimize For...Next loop
-
Apr 16th, 2017, 08:22 PM
#24
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
I checked " Project menu => Properties... and see Application page" and see .NET Framework 4.5.2. So other computers need to have .NET Framework 4.5.2 already installed on them before I can run the portable .exe?
I just tested your refined code above and it works fine. However, the banner text appears in orange on white background. I would like to have "black text" on white background.
How could I change text color?
Thank you
Last edited by NeedHelpVB; Apr 16th, 2017 at 08:57 PM.
-
Apr 16th, 2017, 10:11 PM
#25
Re: Need help in how to add banner information onto JPEG (JPG) images ???
I checked " Project menu => Properties... and see Application page" and see .NET Framework 4.5.2. So other computers need to have .NET Framework 4.5.2 already installed on them before I can run the portable .exe?
Yes.
I just tested your refined code above and it works fine. However, the banner text appears in orange on white background. I would like to have "black text" on white background.
How could I change text color?
The new Banner class has two properties Color and ShadowColor, set them to your desired, in your case replace
Code:
cBanner.Color = Brushes.Red
cBanner.ShadowColor = Brushes.Yellow
With
Code:
cBanner.Color = Brushes.Black
cBanner.ShadowColor = Brushes.White
-
Apr 17th, 2017, 06:12 AM
#26
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Thank you. I will try different colors to have better image of text.
I see you use " Me. " code a few times. What does it do in coding general or in VB .NET programming?
Have a great day.
-
Apr 17th, 2017, 08:31 AM
#27
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by NeedHelpVB
I see you use " Me. " code a few times. What does it do in coding general or in VB .NET programming?
It is VB specific... it refers to "the current instance of this class".
For code inside the Banner class, it is referring to the Banner class... but only for the current instance.
The example in post #23 creates one instance of the Banner class (Dim cBanner As New Banner), but it could also create more (perhaps Dim cBanner2 As New Banner etc). Using Me.Font etc helps ensure that the values are assigned to the correct instance, and more importantly helps with clarity of reading (because you know it belongs to the current class) and ease of writing.
-
Apr 17th, 2017, 02:41 PM
#28
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Thanks for your explanation.
Is there a way to write/convert jpg images directly to pdf file format and combined those individual converted pdf's to a single pdf file in sequential page # in VB .NET?
Regards
Last edited by NeedHelpVB; Apr 17th, 2017 at 02:44 PM.
-
Apr 17th, 2017, 04:32 PM
#29
Re: Need help in how to add banner information onto JPEG (JPG) images ???
That is unrelated to the initial question in this thread, so doesn't really belong here.
It would be much better to create a new thread for that question for several reasons, such as you are more likely to get good help (as many people will not join in with an ongoing thread, especially when it has lots of posts), and it is easier for people to find answers if they search (which many people do).
-
Apr 17th, 2017, 10:15 PM
#30
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
Hi 4x2y,
The banner data print text so close to the edge of paper. Please see below. I have tried to changed a couple number in your refined code but it has not been work.
Which parameters/numbers should I change to get the end of text a few characters back from the edge of paper?
Thank you
-
Apr 17th, 2017, 11:28 PM
#31
Re: Need help in how to add banner information onto JPEG (JPG) images ???
You can change font name and size from this line
Code:
cBanner.Font = New Font("Courier New", 8, FontStyle.Regular)
But i see you already use small font size, are you need the full path or you can just print the file name only?
-
Apr 18th, 2017, 06:04 AM
#32
Thread Starter
Lively Member
Re: Need help in how to add banner information onto JPEG (JPG) images ???
I need full path. I did change the font size to 8 but I guess the banner text changes its starting position dynamically. That is why end of the first text line (...ending with .JPG) is still so close to the edge of paper.
Anyway, thank you for your help so far. You are helpful and wish you have a great great day.
-
Apr 18th, 2017, 03:38 PM
#33
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
Try different font name, i see you are using the one i defined "Courier New" which is wider than others because it is fixed-width font, i suggest using "Segoe UI" if it is available in your system, otherwise use similar one like "Tahoma"
Code:
cBanner.Font = New Font("Segoe UI", 8)
Last edited by 4x2y; Apr 18th, 2017 at 04:01 PM.
-
Apr 19th, 2017, 06:36 AM
#34
Thread Starter
Lively Member
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
Hi 4x2y, I will try and let you know. Thanks again for your help
-
Apr 21st, 2017, 08:45 AM
#35
Thread Starter
Lively Member
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
Hello 4x2y,
I tried your suggested font, it works a bit better. Thank you again.
I have another small issue below and wonder if you can help me on it.
I have lots of jpeg images have text and images displayed in landscape mode (or horizontally).
Is there a VB. NET code/way to rotate the images 90 degrees so that the images/text display in "portrait" modes before saving back to jpeg file format
Thank you in advance
-
Apr 21st, 2017, 10:01 AM
#36
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
Originally Posted by NeedHelpVB
I have lots of jpeg images have text and images displayed in landscape mode (or horizontally).
Is there a VB. NET code/way to rotate the images 90 degrees so that the images/text display in "portrait" modes before saving back to jpeg file format
That's a new question that deserves its own thread. I suggest posting that question in either in the Vb.NET section, or in the Graphics section (making sure you specify what version/edition of VB you're using.
-tg
-
Apr 24th, 2017, 09:51 PM
#37
Thread Starter
Lively Member
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
Hi 4x2y,
I have a VB .NET project with a few modules inside it. I want to incorporate your banner code (as seen above) to my project by doing below:
1) Change Private Sub PrintBannerData_Click(sender As Object, e As EventArgs) Handles Button1.Click
To
Private SubPrintBannerData(sender As Object, e As EventArgs)
2) After make change above, I add your whole to a separate module inside my VB .NET project I said above.
3) I added same reference libraries as seen in your codes to my VB .NET project.
However, there are lots of errors such as
Type "Font" is not define
Type "Image" is not defined
Brushes is not declared.
Cursor is not a member of Form 1
Cursor is not declare
and many other errors
So, how could I add your code to my project correctly?
Your help is greatly appreciated. Have a good day!
-
Apr 25th, 2017, 03:52 PM
#38
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
There are two parts, the Banner's code and calling that code from your form,
to separate:
(1) Open your project, go to Project menu, select Add Class... and name it Banner then paste the following
Code:
Option Strict On
Option Explicit On
Public Class Banner
Public Enum Direction
Vertical
Horizontal
End Enum
Public Text As String
Public Font As Font
Public TextDirection As Direction
Public Color As Brush
Public ShadowColor As Brush
Public Sub New()
' Set default values
Me.Text = "Banner"
Me.TextDirection = Direction.Vertical
Me.Font = New Font("Arial", 8, FontStyle.Regular)
Me.Color = Brushes.White
Me.ShadowColor = Brushes.Black
End Sub
Public Sub Draw(ByVal cTargetImage As Image)
Try
Dim gfx As Graphics = Graphics.FromImage(cTargetImage)
Dim cSizeF As SizeF = gfx.MeasureString(Me.Text, Me.Font)
Select Case Me.TextDirection
Case Direction.Vertical
Dim cFormat As StringFormat = New StringFormat With {.FormatFlags = StringFormatFlags.DirectionVertical Or StringFormatFlags.DirectionRightToLeft}
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, cSizeF.Height + 1, cTargetImage.Height - cSizeF.Width - 1, cFormat)
gfx.DrawString(Me.Text, Me.Font, Me.Color, cSizeF.Height, cTargetImage.Height - cSizeF.Width, cFormat)
Case Direction.Horizontal
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, 16, cTargetImage.Height - cSizeF.Height - 1)
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, 15, cTargetImage.Height - cSizeF.Height)
End Select
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
(2) In the form where your button located, paste the following under its Click event
Code:
Dim cImage As Image
Dim cBanner As New Banner
Dim t As Date = Now ' store date, so it is not change inside the loop
Dim strFiles() As String = IO.Directory.GetFiles("replace_with_your_folder_path", "*.jpg", IO.SearchOption.TopDirectoryOnly)
cBanner.TextDirection = Banner.Direction.Vertical
cBanner.Font = New Font("Segoe UI", 8, FontStyle.Regular)
cBanner.Color = Brushes.Black
cBanner.ShadowColor = Brushes.White
Me.Cursor = Cursors.WaitCursor
For j = 0 To strFiles.Length - 1
cImage = Image.FromFile(strFiles(j))
cBanner.Text = strFiles(j) & ControlChars.NewLine &
t.ToString & ControlChars.NewLine &
"Printed by " & Environment.UserName & " \ Page " & j + 1 & " Of " & strFiles.Length
cBanner.Draw(cImage)
cImage.Save(strFiles(j) & "_with_banner.jpg", Imaging.ImageFormat.Jpeg) ' save copy of the picture
Next
Me.Cursor = Cursors.Default
End Sub
That's all.
-
Apr 25th, 2017, 08:54 PM
#39
Thread Starter
Lively Member
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
Hi 4x2y, I did exactly what you said above but same errors appear as I said previously.
Could you please write down the whole code (class sub and form sub)?
Also, I do not use "Form" application in my project. Instead, I use Console application to create modules and sub's to build up my project.
In my project, it has a Sub Main() which then calls multiple "public sub's and private sub's" in multiple modules. My project's objective is to save drawings into jpeg, then use/call your code to add banner text and save the images back into jpeg again.
Please help me by re-writing your code in "module format" so that I can hopefully include them (banner class) to my project.
Thank you in advance.
-
Apr 25th, 2017, 10:29 PM
#40
Re: [RESOLVED] Need help in how to add banner information onto JPEG (JPG) images ???
I see, then follow these steps
(1) Open your project, go to Project menu, select Add Reference... and select System.Drawing
(2) Go to Project menu again, select Add Class... and name it Banner then paste the following
Code:
Option Strict On
Option Explicit On
Imports System.Drawing
Public Class Banner
Public Enum Direction
Vertical
Horizontal
End Enum
Public Text As String
Public Font As Font
Public TextDirection As Direction
Public Color As Brush
Public ShadowColor As Brush
Public Sub New()
' Set default values
Me.Text = "Banner"
Me.TextDirection = Direction.Vertical
Me.Font = New Font("Arial", 8, FontStyle.Regular)
Me.Color = Brushes.White
Me.ShadowColor = Brushes.Black
End Sub
Public Sub Draw(ByVal cTargetImage As Image)
Try
Dim gfx As Graphics = Graphics.FromImage(cTargetImage)
Dim cSizeF As SizeF = gfx.MeasureString(Me.Text, Me.Font)
Select Case Me.TextDirection
Case Direction.Vertical
Dim cFormat As StringFormat = New StringFormat With {.FormatFlags = StringFormatFlags.DirectionVertical Or StringFormatFlags.DirectionRightToLeft}
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, cSizeF.Height + 1, cTargetImage.Height - cSizeF.Width - 1, cFormat)
gfx.DrawString(Me.Text, Me.Font, Me.Color, cSizeF.Height, cTargetImage.Height - cSizeF.Width, cFormat)
Case Direction.Horizontal
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, 16, cTargetImage.Height - cSizeF.Height - 1)
gfx.DrawString(Me.Text, Me.Font, Me.ShadowColor, 15, cTargetImage.Height - cSizeF.Height)
End Select
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
(3) Add the following method to the same module where Sub Main() located
Code:
Private Sub PrintBannerData()
Dim cImage As Image
Dim cBanner As New Banner
Dim t As Date = Now ' store date, so it is not change inside the loop
Dim strFiles() As String = IO.Directory.GetFiles("replace_with_your_folder_path", "*.jpg", IO.SearchOption.TopDirectoryOnly)
cBanner.TextDirection = Banner.Direction.Vertical
cBanner.Font = New Font("Segoe UI", 8, FontStyle.Regular)
cBanner.Color = Brushes.Black
cBanner.ShadowColor = Brushes.White
Console.WriteLine("Please wait...")
For j = 0 To strFiles.Length - 1
cImage = Image.FromFile(strFiles(j))
cBanner.Text = strFiles(j) & ControlChars.NewLine &
t.ToString & ControlChars.NewLine &
"Printed by " & Environment.UserName & " \ Page " & j + 1 & " Of " & strFiles.Length
cBanner.Draw(cImage)
cImage.Save(strFiles(j) & "_with_banner.jpg", Imaging.ImageFormat.Jpeg) ' save copy of the picture
Console.Write(".") ' instead of Me.Cursor = Cursors.WaitCursor
Next
Console.WriteLine()
Console.WriteLine("Done!")
Console.Read()' don't close console window
End Sub
(4) Add Imports System.Drawing at the top of your module
(5) Call PrintBannerData from anywhere in your module, e.g.
Code:
Sub Main()
' previous code
'...
'...
'...
PrintBannerData()
End Sub
Last edited by 4x2y; Apr 25th, 2017 at 10:35 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
|