VB.net equivalent to Excel Shape - Circle/Ellipse with Beveling and Gradient - vb.net

Ellipse Object Created in Excel with Beveling, Gradient, etc.
Can this be achieved in VB.Net Windows App Form? Right now I am using:
Dim Disc1 As New Drawing2D.GraphicsPath
Disc1.AddEllipse(1, 1, 138, 138)
Panel1.Region = New Region(Disc1)
Any advice that you can give will be greatly appreciated.

Related

Saving chart in userform as picture

A part of this question concerns doing the reverse of the following one:
VB.net get location of userControl in another container
No matter what I do, I cannot seem to pinpoint the location of my chart inside a userform (D inside A), therefore what I save is something very different than the chart alone.
So, provided that my userform is too like this:
I need to save a screenshot of D, being D a polar chart with a title and a legend.
My code is:
Dim PicFile As String = FilesFolder & "Pic.png"
Dim myBounds As Rectangle = A.D.Bounds
Dim BitMap As New Bitmap(myBounds.Width, myBounds.Height)
Dim PtChart As Point
PtChart = A.PointToScreen(New Point(0, 0))
PtChart = A.D.PointToClient(PtChart)
Using g As Graphics = Graphics.FromImage(BitMap)
g.CopyFromScreen(PtChart, Point.Empty, myBounds.Size)
End Using
BitMap.Save(PicFile, System.Drawing.Imaging.ImageFormat.Png)
Why does this not save the chart (D) correctly?
BONUS: how can I make it work even when I have another application open (say, internet browser) on top of the userform?

How to change font to Calibri in PDF using iTextsharp for VB.NET

I am trying to change the font in a PDF file while using VB.net and itextsharp.
I think I am allmost there but I am failing at declaring the right things.
The code I got looks ok but I get an error.
Dim FontColour As VariantType = New BaseColor(35, 31, 32)
Dim Calibri8 As VariantType = FontFactory.GetFont("Calibri", 8, FontColour)
The errors I get are the following.
Severity Code Description Project File Line Suppression State
Error BC30311 Value of type 'BaseColor' cannot be converted to 'VariantType'. J C:\Users\Jeffrey\Documents\Visual Studio 2013\Projects\J\J\UniFormulieren\Offerte\OfferteUniverseel.vb 61 Active
Severity Code Description Project File Line Suppression State
Error BC30311 Value of type 'Font' cannot be converted to 'VariantType'. J C:\Users\Jeffrey\Documents\Visual Studio 2013\Projects\J\J\UniFormulieren\Offerte\OfferteUniverseel.vb 62 Active
I also tried using another method as below.
Dim bftimes As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, False)
Dim times As New Font(bftimes, 10)
This way I could introduce "Times new roman" but not Calibri.
The code above would only let met add Times new roman, Courier and Helvetica.
I am using Microsoft Visual studio 2017 with VB.NET and iTextSharp.
My C# is not that great, but it helped me get this far in VB.net :)
Edit:
The first solution is something I tried using this answer from Nalan M.
https://stackoverflow.com/a/29641544/9056174
Edit2:
SOLUTION
Thank you for your help ! I was so hammering on that varianttype that I was blind for another option.
Made the follow adjustments and indeed it worked like a charm!
Dim FontColour As BaseColor = New BaseColor(35, 31, 32)
Dim Calibri8 As Font = FontFactory.GetFont("Calibri", 8, FontColour)
Thank you guys for your help. ( Still don't get why I didn't thought of it myself, using the C# to VB.net converter sometimes makes you crazy)
The error messages
Value of type 'BaseColor' cannot be converted to 'VariantType'
and
Value of type 'Font' cannot be converted to 'VariantType'
indicate what the issue is: The BaseColor and Font instances cannot be converted to a VariantType.
Thus, one has to assign them to variables of a different type. Fortunately there are natural alternatives: Let's take the known actual types BaseColor and Font!
Dim FontColour As BaseColor = New BaseColor(35, 31, 32)
Dim Calibri8 As Font = FontFactory.GetFont("Calibri", 8, FontColour)

Add a gradient to a shape/line

I am attempting to add a gradient to a line shape in Excel using VBA. This feature is available in the Line Color section under the Format Shape option. Despite this feature existing under the Format Shape option, I am unable to reproduce the functionality in VBA. My code is:
With ActiveSheet.Shapes("Straight Connector 4")
.Line.ForeColor.RGB = RGB(193, 193, 193)
.Line.Transparency = 0.25
.Line.Visible = msoTrue
.Line.ForeColor.SchemeColor = 24
.Line.BackColor.SchemeColor = 34
.Line.GradientStops.Insert RGB(255, 0, 0), 0.25 ' Creates error
.Line.Gradient.ColorStops.Add (1) ' Creates error
End With
I know you can easily add a gradient to the shape fill but all search results are returning nothing when wanting to add a gradient to a shape line. Any ideas are more than welcome.
As far as I know, it is not possible. You can set a gradient for a shape's fill through VBA, but you can't do it for a line.
You can either create a thin shape with gradient fill and no border, or you will have to use something outside VBA. (Eg. VB.NET + OpenXLM SDK.)

Find Bounds of GDI+ Shape

Hi I'm trying to get a rectangle that completely surrounds a shape drawn via GDI+ when the pen is thick.
This is my test code to demo which I just added to mybase.load to test out..
Dim bmp As New Bitmap(500, 500)
Dim g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.Transparent)
g.SmoothingMode = SmoothingMode.HighQuality
g.PixelOffsetMode = PixelOffsetMode.Default
' Setup Matrix
Dim mat As New Matrix
mat.Translate(100, 100)
g.Transform = mat
' Draw Lines and Fill
Dim gp As New GraphicsPath
gp.AddLines({New Point(50, 0), New Point(100, 100), New Point(0, 100)})
gp.CloseAllFigures() ' Closes Open Path (ie join back to origin)
g.FillPath(New SolidBrush(Color.Beige), gp)
' Add Border to Graphics Path
g.DrawPath(New Pen(New SolidBrush(Color.Black), 20), gp)
g.DrawRectangle(New Pen(New SolidBrush(Color.Red), 1), Rectangle.Truncate(gp.GetBounds))
' Tidy Up
g.ResetTransform()
mat.Dispose()
' Set Picturebox Value
PictureBox1.Image = bmp
I'd post the image but the forum doesn't let me.
I want the bounding rectangle to include the pen width too. I used the above triangle to demo that I cant just add penwidth/2.
Any ideas?
Julian
If that is sufficient for your needs you can simply set the pen's .Alignment property to Inset like so:
g.DrawPath(New Pen(New SolidBrush(Color.Black), 20) With {.Alignment = PenAlignment.Inset}, gp)
This basically tells the pen to draw itself on the inside of the path at all times. Default is that it draws itself on the middle which leads to the situation that the outside width at edges is dependant on the angle of the edge.
Result is:
Just in case anyone else struggles like i did trying to find a solution then this is what I did in the end. I gave up trying to do using some form of getbounds method.
My solution in the was to scan the rendered bitmap instead to calculate the bounds based on pixels that were non-transparent (the background of my rendered image(s)). In fact I only need to scan once on the combined image and it takes a fraction of a second to calculate the "true" bounds now.
I used this article Fit a PictureBox to its non-transparent pixels to achieve a starting point that proved the concept and gave me a region that I could do a "getbounds" on. Then I swapped out the slow getpixel method for the lockbits example at Work with bitmaps faster in C# and voila!
Just ran all the code through an online c# to vb convertor and pasted it together to get completely working in less than an hour.
Took a fraction of the time I wasted trying to find a solution on the internet that would work directly on the vector image.
Julian

Adding multiple pictureboxes to a form programmatically in vb.net

So I have written a short section of code to add 6 pictureboxes to a form in random locations. It adds each picturebox to a collection, then loops through the collection and adds them to the form control. The bizarre issue is that the code only works when I step through it line by line in debug mode. If I just compile and run the code then only 1 picturebox is added to the form, but if I step through the code line by line then all 6 pictureboxes are successfully added to the form in random locations. Can anyone tell me why the hell this happening? It's driving me pretty nuts. Code below:
For i As Integer = 0 To 5
Dim pic As New PictureBox
Dim rnd As New Random
pic.Location = New Point(rnd.Next(200, 300), rnd.Next(200, 300))
pic.Size = New Size(5, 5)
pic.BackColor = Color.White
pic.Visible = True
pic.BringToFront()
_picCollection.Add(pic)
Next
For Each item As PictureBox In _picCollection
Controls.Add(item)
Next
ShowDialog()
Open to suggestions of how to do this better / in a way that actually works properly.
Had to declare RND object outside of loop. Thanks tinstaafl!