string with surrounding color - vb.net

i want to make an image in vb.net which is a string
it should be made of 2 colors one as forecolor the other as a color surrounding the first one
how should i make it using code?
my result must be some thing like this image(yellow as forecolor and red! as background)
[the string is in persian]
right now i first make the string using
Dim result As New Bitmap(100, 100)
Dim g As Graphics = Graphics.FromImage(result)
g.DrawString("My string", New Font("Arial", 40), New SolidBrush(Color.yellow), 22, 22)
and then process this image by checking every single pixel and if they are close to the string i color them as red , the code is this
kr = font_color.R
kg = font_color.G
kb = font_color.B
For i = 0 To (img.Height - 1) Step 1
prg.Value = prg.Value + 1
For j = 0 To (img.Width - 1)
If (kr = img.GetPixel(j, i).R And kg = img.GetPixel(j, i).G And kb = img.GetPixel(j, i).B) Then
'some code
ElseIf (isnabor(j, i) = True) Then'checks if it is close enough or not
img.SetPixel(j, i, back_color)
Else
img.SetPixel(j, i, Color.Transparent)
End If
Next
Next
The problem is that it takes a long time for a large image
any better way?

Try using GraphicsPath. Check the following links for more information
www.codeproject.com/Articles/42529/Outline-Text
www.java2s.com/Tutorial/VB/0300__2D-Graphics/Textoutline.htm
www.java2s.com/Tutorial/VB/0300__2D-Graphics/AddstringtoGraphicsPath.htm
Bob Powell: Text Effects

by the help of my friend i found the answer here it is:
Dim result As New Bitmap(1000, 1000)
Dim grp As Graphics = Graphics.FromImage(result)
Dim gp As New Drawing2D.GraphicsPath
Dim useFont As Font = New Font("IranNastaliq", 100, FontStyle.Regular)
grp.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
grp.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gp.AddString(rr.Lines(aa), useFont.FontFamily, FontStyle.Regular, 100, New Point(0, 0), StringFormat.GenericTypographic)
useFont.Dispose()
grp.FillPath(Brushes.White, gp)
grp.DrawPath(Pens.Black, gp)
gp.Dispose()
pic.Image = result

Related

How to get rid of pixelated edges of a jpg?

For my student project, I’ve a vb.net code snippet that downloads a jpg file from a website and then sets all its pixels, which are almost white, entirely white:
Dim oColor As Color
Dim bRed As Byte
Dim bGreen As Byte
Dim bBlue As Byte
Dim imgTemp As Image = Image.FromFile("C:\DownloadedImage.jpg")
Dim bmpTemp As New Bitmap(imgTemp.Width, imgTemp.Height, Imaging.PixelFormat.Format32bppArgb)
Using gfx As Graphics = Graphics.FromImage(bmpTemp)
gfx.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gfx.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
gfx.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
gfx.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
gfx.DrawImage(imgTemp, 0, 0)
End Using
For x = 0 To bmpTemp.Width - 1
For y = 0 To bmpTemp.Height - 1
oColor = bmpTemp.GetPixel(x, y)
bRed = oColor.R
bGreen = oColor.G
bBlue = oColor.B
If bRed <= 254 AndAlso bRed >= 254 - 20 Then '20 = White Tolerance'
bmpTemp.SetPixel(x, y, Color.White)
End If
If bGreen <= 254 AndAlso bGreen >= 254 - 20 Then
bmpTemp.SetPixel(x, y, Color.White)
End If
If bBlue <= 254 AndAlso bBlue >= 254 - 20 Then
bmpTemp.SetPixel(x, y, Color.White)
End If
Next
Next
Dim newForm As New Form With {.BackgroundImage = bmpTemp, .TransparencyKey = Color.White, .BackgroundImageLayout = ImageLayout.Zoom, .BackColor = Color.White}
newForm.Show()
The bitmap created in this way is then set as the BackgroundImage of a form whose TransparencyKey property is white. The result is not a complete failure, but the edges of the isolated object are still white and pixelated:
The result in two different sizes (left) and what I'm trying to achieve (right)
I’m now looking for a way to get rid of those ragged edges, something like Photoshop’s “Refine Edge” function.
Many thanks in advance!

Creating a chart in VisualBasic.net which has a Y axis comprising of string values

I'd like to create a chart in VB.net which has X and Y values in the form of a string.
Example below:
Sorry for the bad drawing, but this is what I'd like it to do
Can anybody help me find the settings to do this?
I can transfer data to a graph but it ends up looking like this:
This is what my current graph looks like. As you can see, I can't get the Y axis working.
(also dont worry about the X axis containing the grades, that's just something I need to fix. The grades string() still contains the right data)
This is a sample of my code: (don't worry about delimitegrades(), that just formats data for the grades into 'A', 'B', etc)
Subjects is a string() list.
Grades is an array which contains the data I need to insert. ChrtSubgrade is the chart itself.
Public Sub CreateGraph(ByVal name As String, subjects() As String)
MsgBox("Generating graph for " & name)
chrtSubgrade.Series.Clear()
chrtSubgrade.Series.Add("Data")
chrtSubgrade.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Column
chrtSubgrade.Series(0).YValueType = DataVisualization.Charting.ChartValueType.String
chrtSubgrade.Series(0).IsValueShownAsLabel = True
delimitgrades(subjects)
For i = 0 To subjects.Count - 2
chrtSubgrade.Series(0).Points.AddXY(subjects(i), grades(i))
Next
I've breakpointed all the code and the arrays and data transfer is fine so I believe it's just down to how I'm creating the graph.
I can't link the chart to a database since I'm drawing the source data from an XML file.
Thanks very much for any help you might be able to give.
I figured out a new method. Instead of using the VB charting tools I made my own. Here's the code:
Public Sub CreateGraph(ByVal name As String, subjects() As String)
delimitgrades(subjects) 'collects the info from subjects which contains the grade data and make a new array from it containing only grades
MsgBox("Generating graph for " & name)
Dim mygraphics As Graphics = Graphics.FromHwnd(hwnd:=ActiveForm.Handle) 'defines a new graphics set on the Grades window
Dim axespen As New Pen(Color.Black, 5) 'makes a pen so I can draw the axes
Dim Xaxisleft As New Point(30, 350) 'defines the left point of the X axis
Dim Xaxisright As New Point(400, 350) 'defines the right point of the X axis
Dim Yaxisbottom As New Point(30, 350) 'defines the bottom point of the Y axis
Dim yaxistop As New Point(30, 80) 'defines the top point of the Y axis
For i = 0 To 4 'for each possible grade - A* through D
Dim labelgrade As New Label 'makes a label
With labelgrade 'with said label
.BackColor = Color.Transparent 'makes its background colourless
.AutoSize = True 'resizes the bounding box
Select Case i 'examines I - the counting variable
Case Is = 0 ' if its 0
.Text = "A*" ' sets the labels text to A*
.Location = New Point(2, 100) 'moves it to the right place
Case Is = 1 'etc
.Text = "A"
.Location = New Point(2, 140)
Case Is = 2
.Text = "B"
.Location = New Point(2, 180)
Case Is = 3
.Text = "C"
.Location = New Point(2, 220)
Case Is = 4
.Text = "D"
.Location = New Point(2, 260)
End Select '/etc
End With
Controls.Add(labelgrade) 'inserts the label into the form
Next
For i = 0 To subjects.Count - 2 'the last part of the subjects array is empty so it counts to the last position containing data
Dim labelsubject As New Label 'makes a new label
Dim labelxoffset As Integer = 30 'defines the variable xoffset which is used to determine where all labels should be placed
With labelsubject 'with this label
.BackColor = Color.Transparent 'make the background colourless
.AutoSize = True 'resize the bounding box
Select Case i 'examine i
Case Is = 0 'if this is the first label placed onto the form
.Text = subjects(i) 'take the first entry in the subjects array
.Location = New Point(30, 355) 'place the label on the X axis in the first position
Case Else 'otherwise
.Text = subjects(i) 'take the right name from the array and make the label reflect this name
.Location = New Point(labelxoffset + (i * 70), 365) 'set its location depending on which place it is in the array using xoffset
End Select
End With
Controls.Add(labelsubject) 'add the label to the form
Next
'Axes
mygraphics.DrawLine(axespen, Xaxisleft, Xaxisright) 'create
mygraphics.DrawLine(axespen, Yaxisbottom, yaxistop) 'the axes
'bars
For i = 0 To grades.Count - 1 'from 0 to the second to last entry in the grades array (this is because of how I formed the grades array. It still works.
Dim grade As String = grades(i) 'create a temp variable to store the correct entry from the grades array
Dim gradeindex As Integer = Nothing ' create the index integer, this is used to determine how high the bar should go
Dim gradepen As New Pen(Color.Green, 50) 'make the pen for the bars
Dim p1 As New Point(30, 350) 'define point 1 as above the first label
Dim p2 As New Point 'create point 2
Dim x As Integer = (58 + (70 * i)) 'create a new xoffset integer
Dim yoffset As Integer = 348 ' create the yoffset integer. This is to place the bottom point of the bar correctly
Select Case grade 'examine grade
Case Is = "A*" 'if its A*
gradeindex = 100 'the top y coord is 100
p1 = New Point(x, yoffset) 'p1 is now x, yoffset
p2 = New Point(x, gradeindex) 'p2 is now x, gradeindex
Case Is = "A" 'etc
gradeindex = 140
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
Case Is = "B"
gradeindex = 180
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
Case Is = "C"
gradeindex = 220
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
gradepen = New Pen(Color.Orange, 50) 'make the grade pen orange
Case Is = "D"
gradeindex = 260
p1 = New Point(x, yoffset)
p2 = New Point(x, gradeindex)
gradepen = New Pen(Color.Red, 50) 'make the grade pen red
End Select '/etc
mygraphics.DrawLine(gradepen, p1, p2) 'draw the line from p1 to p2
Next
End Sub
Here's the output for someone who has the following data: German;A* , Maths;A , French;A* , Bio;C , Bus(business for example);B
Output
Hope this helps anyone else having this issue. Thanks!

Custom colors on bar chart using Zedgraph library

Am using Zedgraph chart library but I can't seem to be able to color a bar graph depending on a specific condition. Am following along with the example found on this tutorial.
In my case, if the value isn't above 50 -which is the student.pass_mark variable-, I want to color the bar red and if its above 50 I want to color it green. Below is my code. Which so far only gives me red even though I have values of 100, 80, 110 etc.
Dim subject As String
Dim grade As Decimal
Dim colors As Color() = {}
Dim subject_names As String() = {}
For i = 0 To student.no_of_subjects
ReDim Preserve colors(i)
ReDim Preserve subject_names(i)
subject = student.subject_name
grade = student.grade
Dim x As Double = CDbl(i) + 1
Dim y As Double = grade
Dim z As Double = 0
list.Add(x, y, z)
If grade < student.pass_mark Then
colors(i) = Color.Red
Else
colors(i) = Color.Green
End If
subject_names(i) = subject
Next
Dim myCurve As BarItem = myPane.AddBar("Student Subject", list, Color.Blue)
'Dim colors As Color() = {Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Purple}
myCurve.Bar.Fill = New Fill(colors)
myCurve.Bar.Fill.Type = FillType.Solid
myCurve.Bar.Fill.RangeMin = 0
myCurve.Bar.Fill.RangeMax = 4
myPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45)
myPane.Fill = New Fill(Color.White, Color.FromArgb(255, 255, 225), 45)
' Tell ZedGraph to calculate the axis ranges
' Set the XAxis labels
myPane.XAxis.Scale.TextLabels = subject_names
' Set the XAxis to Text type
myPane.XAxis.Type = ZedGraph.AxisType.Text
ZedChart.IsShowPointValues = True
ZedChart.AxisChange()
ZedChart.Refresh()
Also, I want to draw a line across the whole chart that shows the pass_mark so that it is quickly visible that a student' has or hasn't passed a certain subject in comparison to the passmark
I don't know whether it is still relative but you can do multi-colored bar plots in ZedGraph.
You can find the tutorial here:
http://zedgraph.dariowiz.com/indexb806.html?title=Multi-Colored_Bar_Demo
In your case you can use FillType.GradientByColorValue I assume.
For the line you can simply add a LineObj to the myPane.GraphObjList

Print multiple images in vb.net

In VB.NET, I need to print multiple Images of bar codes by arranging them in tabular format. For now what I am doing is, Creating the bar codes and adding them in new picture box. These Picture boxes are added on a panel which I am creating on form at run time and print that panel (with picture boxes in 4x9 table).
But, when I need to print more that 36 bar codes, this idea doesn't work.
So, Please suggest me some improvements in my code or any other way to do this job.
I am sorry, here is the code for generating images and adding them to the panel..
''' Method for create bar code images with a loop and adding them to the panel by picture box...
Private Function GetBarcodeText(RowId As Guid)
Dim BarcodeValue As StringBuilder = New StringBuilder(96)
Dim temp As New StringBuilder
Dim data As String
Dim param = New SqlParameter()
Dim imageNo As Integer = 0
Dim colorValue As String = ""
Dim scaleValue As String = ""
'' Adding the panel on the form which is dynamically created
Me.Controls.Add(outputPanel)
'' Setting the Initial size for the panel
outputPanel.Size = New Point(794, 112)
outputPanel.Name = "outputPanel"
outputPanel.BackColor = Drawing.Color.White
param.ParameterName = "#RowId"
param.Value = RowId
param.SqlDbType = SqlDbType.UniqueIdentifier
' Get the particular row of data from database
dt = objStockProvider.GetBarcodeDetails(param)
' GET colour code
Dim color As String = dt.Rows(0)("COLOUR").ToString()
Dim countColors As Integer = 0
' Get the color code numbers
param.ParameterName = "#Dscale"
param.Value = dgvViewTickets.CurrentRow.Cells("SCALE").Value.ToString()
countColors = objStockProvider.CountColorCodes(param)
For i = 1 To countColors
For j As Integer = 1 + ((12 / countColors) * (i - 1)) To (12 / countColors) * i
If dt.Rows(0)("S" + j.ToString()) <> 0 Then
Dim totalTicketsForASize As Integer
totalTicketsForASize = dt.Rows(0)("S" + j.ToString())
For k As Integer = 1 To totalTicketsForASize
' Set Bar code value which has to create
BarcodeValue = "123456789012"
' Create Barcode Image for given value
Dim image = GetBarcodeImage(BarcodeValue, colorValue, scaleValue)
If image IsNot Nothing Then
'' Create picture box to contain generated Image.
Dim pcbImage As New PictureBox
pcbImage.Width = W
pcbImage.Height = H
pcbImage.Image = image
pcbImage.Location = New Point(X, Y)
imageNo += 1
If imageNo Mod 4 = 0 Then
X = 15
Y += H
outputPanel.Height += H
Else
X += W
Y = Y
End If
pcbImage.Visible = True
'' Adding picture box to panel
outputPanel.Controls.Add(pcbImage)
End If
Next
End If
Next
color = color.Substring(color.IndexOf(",") + 1, color.Length - color.IndexOf(",") - 1)
Next
PrintGeneratedTickets()
End Function
Now, I am printing the panel by following method:
Private Sub PrintGeneratedTickets()
bmp = New Bitmap(outputPanel.DisplayRectangle.Width, outputPanel.DisplayRectangle.Height)
Dim G As Graphics = Graphics.FromImage(bmp)
G.DrawRectangle(Pens.White, New Rectangle(0, 0, Me.outputPanel.DisplayRectangle.Width, Me.outputPanel.DisplayRectangle.Height))
Dim Hdc As IntPtr = G.GetHdc()
SendMessage(outputPanel.Handle, WM_PRINT, Hdc, DrawingOptions.PRF_OWNED Or DrawingOptions.PRF_CHILDREN Or DrawingOptions.PRF_CLIENT Or DrawingOptions.PRF_NONCLIENT)
G.ReleaseHdc(Hdc)
pndocument.DocumentName = bmp.ToString()
Dim previewmode As New PrintPreviewDialog
previewmode.Document = pndocument
previewmode.WindowState = FormWindowState.Maximized
previewmode.PrintPreviewControl.Zoom = 1
pndocument.DefaultPageSettings.Margins.Top = 10
pndocument.DefaultPageSettings.Margins.Bottom = 30
pndocument.DefaultPageSettings.Margins.Left = 16
pndocument.DefaultPageSettings.Margins.Right = 16
pndocument.DefaultPageSettings.Landscape = False
' Set other properties.
previewmode.PrintPreviewControl.Columns = 4
previewmode.PrintPreviewControl.Rows = 9
previewmode.ShowDialog()
Dim file As String = DateTime.Now.ToString()
file = Path.GetFullPath("D:\Bar Codes\" + file.Replace("/", "-").Replace(":", ".") + ".bmp")
bmp.Save(file)
G.Dispose()
outputPanel.Controls.Clear()
End Sub
This code is working fine but what I need to do, is to fix the number of images (4x9) per page. But when I am trying to create more than it, that all are printing on a single page with compressed size.
Also when trying to re-run the code, It shows nothing in preview..
Some body please suggest the correction in code so that I can reprint the tickets and use paging for more than 36 images.
Well, Printing the Images on a panel was not a good idea.. I replaced the panel and created an array of images and used the print document directly and print after arranging images on it.
Thanks.

Combine 2 picture boxes into a new picture box

I have my pixBox1 which is fix, and unable to change the image
and pixBox2 is not fix which can change there color and rotate in here
i use OpenFileDialog function to put image inside those pixbox
so now how can i combine those two pixbox into my pixbox3?
i try this but it doesn't look like it's going to work:
Dim image As New Bitmap(pixBox1.Image)
Dim image2 As New Bitmap(pixBox2.Image)
Dim Image3 As New Bitmap(300, 300)
Dim g As Graphics = Graphics.FromImage(Image3)
g.DrawImage(image1, New Point(300, 300))
g.DrawImage(image2, New Point(300, 300))
g.Dispose()
g = Nothing
pixBox3.Image = Image3
This is kinda ugly and slow, but it basically sets all the pixels in image2 to be 50% transparent then draws it over the top of Image.
Dim image As New Bitmap(pixBox1.Image)
Dim image2 As New Bitmap(pixBox2.Image)
Dim Image3 As New Bitmap(300, 300)
Using g As Graphics = Graphics.FromImage(Image3)
'make 2nd bmp translucent
For Integer Xcount = 0 To image2.Width - 1
For Integer Ycount = 0 To image2.Height - 1
Dim c as Color = image2.GetPixel(Xcount, Ycount)
c = Color.FromARGB(125, c.R, c.G, c.B) '50% alpha
image2.SetPixel(Xcount, Ycount, c)
Next
Next
g.DrawImage(image1, New Point(0, 0))
g.DrawImage(image2, New Point(0, 0))
End Using
pixBox3.Image = Image3
As a side note, the Using block makes sure g is disposed no matter what happens.