copy windowsform chart to clipboard VB - vb.net

I need to copy a chart (System.Windows.Forms.DataVisualization.Charting.Chart) to clipboard in order to paste it in other application. I use below code but it gives error on the last line for "img" says "Value of Type Bitmap cannot be converted to Bitmapsource"
Any help to rectify the error is highly appreciated.
Private Sub CopyChartBtn_Click(sender As Object, e As RoutedEventArgs) Handles CopyChartBtn.Click
Dim main = DirectCast(Application.Current.MainWindow, MainWindow)
Dim SET_H = DirectCast(main.Frame1.Content, SET_Home)
Dim chart1 = DirectCast(SET_H.Benchchart.Child, System.Windows.Forms.DataVisualization.Charting.Chart)
'save image
Dim img As New System.Drawing.Bitmap(chart1.Width, chart1.Height)
Dim gr As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(img)
chart1.Printing.PrintPaint(gr, New System.Drawing.Rectangle(System.Drawing.Point.Empty, chart1.Size))
Clipboard.SetImage(img)
End Sub
My project is WPF and I am hosting windowsform chart in it. Maybe that is the problem because when I run it in a windows form project it works fine

I used System.Windows.Forms.Clipboard.SetImage(img) and it works.Originally when it is a WPF project it uses System.Windows.Clipboard.SetImage(img) when not using the namespace

Related

Windows forms scalling

I'm trying to avoid wrong form showing when "Scale and layout" in windows screen settings is different than 100%.
I've folloved this LINK, and it works but only in 2 cases
1 - in debugging mode in VS
2 - if i put my *.exe file together with *.manifest and *.config files in same location.
If *.exe is alone then it does not work.
I'm looking for a clue what to do next.
Update
This is how application is run.
Private Sub Monitor_Click(sender As Object, e As EventArgs)
Dim trd As New Thread(AddressOf ShowMonitor)
trd.SetApartmentState(Threading.ApartmentState.STA)
trd.Start()
End Sub
Private Sub ShowMonitor()
Dim frMonitor As System.Reflection.Assembly = System.Reflection.Assembly.Load(File.ReadAllBytes("path to folder with DLL files\Monitor.dll"))
Dim oType As System.Type
Dim pControl As System.Object
oType = frMonitor.GetType("Monitor.frmMonitor")
pControl = Activator.CreateInstance(oType)
Application.Run(pControl)
End Sub
So my "monitor" application works as expected only in 2 cases written above.

Print Custom Label with new Dymo Connect SDK with VB.NET Windows Forms

The following code will print a Label with default text & QR Code image from saved image path on computer, but I need to add replace the text & QR image with a custom text & QR Image. I cannot find any examples online of the new SDK, there is a little reference on the GitHub but cant piece anything together to work.
I Installed NuGet package Dynmo.Connect.SDK
Imports DymoSDK.Implementations
Dim dymoSDKLabel As DymoSDK.Implementations.DymoLabel
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim SelectedPrinter As String = "DYMO LabelWriter 450"
Dim copies As Integer = 1
Dim barcodeGraphsQuality As Boolean = True
dymoSDKLabel = New DymoLabel()
dymoSDKLabel.LoadLabelFromFilePath("C:\Users\User\Documents\QR.dymo")
DymoPrinter.Instance.PrintLabel(dymoSDKLabel, SelectedPrinter, copies, barcodeGraphsQuality)
End Sub
I wanted to say that I figured out the answer to my own problem in case someone is wondering how. I created a label in Dymo Connect software program called label.dymo. I added a TextObject and a ImageObject to the label, positioned in the middle and saved the label. I installed a Free NuGet package called QRCoder and referenced it in this sample code below. The String Variable called id is coming from my database and is the row id for what was just created(last insert row id). The QrCode generates a QRCode matching my id variable and then converts it to base64string because that what format the Dymo ImageObject supports. You can replace the id variable with any number or text as you need for your project.
Imports DymoSDK.Implementations
Imports QRCoder
Dim dymoSDKLabel As DymoSDK.Implementations.DymoLabel
Dim LabelTextObject As DymoSDK.Interfaces.ILabelObject
Dim LabelQRObject As DymoSDK.Interfaces.ILabelObject
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim gen As New QRCodeGenerator
Dim data = gen.CreateQrCode(id, QRCodeGenerator.ECCLevel.Q)
Dim qrCode As New Base64QRCode(data)
Dim base64qrcode As String = qrCode.GetGraphic(20)
Dim SelectedPrinter As String = "DYMO LabelWriter 450"
Dim copies As Integer = 1
Dim barcodeGraphsQuality As Boolean = True
dymoSDKLabel = New DymoLabel()
dymoSDKLabel.LoadLabelFromFilePath("C:\Users\User\Documents\label.dymo")
LabelTextObject = dymoSDKLabel.GetLabelObject("TextObject")
dymoSDKLabel.UpdateLabelObject(LabelTextObject, id)
LabelQRObject = dymoSDKLabel.GetLabelObject("ImageObject")
dymoSDKLabel.UpdateLabelObject(LabelQRObject, base64qrcode)
DymoPrinter.Instance.PrintLabel(dymoSDKLabel, SelectedPrinter, copies, barcodeGraphsQuality)
End Sub

Dinamically load XAML in UserControl for Excel Add In (VB)

I am developing an Excel Add In in which I intend to dinamically load a XAML canvas to a WPF UserControl.
The code below worked fine in a full WPF template...
Imports Microsoft.Win32
Imports System.IO
Class MainWindow
Private Sub btnLoadXAML_Click(sender As Object, e As RoutedEventArgs)
Try
Dim FlDialog As OpenFileDialog = New OpenFileDialog()
FlDialog.ShowDialog()
Dim lFlName As String = FlDialog.FileName
'load selected file
Dim fs As FileStream = New FileStream(lFlName, FileMode.Open)
Dim gridToLoad As New Canvas
With gridToLoad
.Height = 300
.Width = 300
End With
gridToLoad = System.Windows.Markup.XamlReader.Load(fs)
grdLoadXAML.Children.Add(gridToLoad)
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
...but when I use it on a Excel Add In template, I can't refer to a XAML object via VB code, such as:
Dim gridToLoad As New Canvas
I mean "New Canvas" doesn't seem to be "imported" into this kind of template. I couldn't find which library to refer to make this work.
Thanks in advance.
Sorry,
Just a few minutes after I've tried to build the project and Visual Basic itself gave me the answer!
Just had to add reference for System.Xaml (dumb!).
Thanks anyway!! :)
Best.

Vb.Net Control Variable Names

I am trying to create bunch of WebBrowsers with Variable Names; I started with the following code, but seems it has something wrong that I cannot figure our;
The error is in the FIRST PORTION OF THE CODE;
Any help/comment appreciated:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim NumberOfBrowsers As Integer = 12
For Pro As Integer = 1 To NumberOfBrowsers
Dim frmNew As New Form
'------------------------- FIRST PORTION:
Dim MekdamBrowser As New WebBrowser
MekdamBrowser = "WebBrowser" & Pro
frmNew.Controls.Add(MekdamBrowser)
'-------------------------
MekdamBrowser.Location = New System.Drawing.Point(10, 10)
MekdamBrowser.Size = New System.Drawing.Size(300, 300)
MekdamBrowser.Show()
'-------------------------
Next
End Sub
End Class
Thanks
It seems that you want the first portion to be as follow instead :
'------------------------- FIRST PORTION:
Dim MekdamBrowser As New WebBrowser
MekdamBrowser.Name = "WebBrowser" & Pro
frmNew.Controls.Add(MekdamBrowser)
'-------------------------
The difference between this and the original code you tried is, above code assigns name for WebBrowser control, where corresponding line of code in question tried to "replace" WebBrowser control it self with a name (it tried to assign string data to variable of type WebBrowser which is not a valid operation).

Can I get information from Excel without Microsoft Interop

So, I'm very limited with what I can do with vb at work. The edition of vb.net they set me up with doesn't have the files to use the microsoft.office things, and they won't give me permissions to download them. Is there any other way to pull all the information in column E of a spreadsheet?
Thank you for any insight you might be able to give me.
You can use late binding. From the linked page:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim objApp As Object
Dim objBook As Object
Dim objBooks As Object
Dim objSheets As Object
Dim objSheet As Object
Dim range As Object
' Instantiate Excel and start a new workbook.
objApp = CreateObject("Excel.Application")
objBooks = objApp.Workbooks
objBook = objBooks.Add
objSheets = objBook.Worksheets
objSheet = objSheets.Item(1)
range = objSheet.Range("A1")
'Set the range value.
range.Value = "Hello, World!"
'Return control of Excel to the user.
objApp.Visible = True
objApp.UserControl = True
End Sub
#KenWhite gave the right answer in regards to Excel without Interop, but all you really needed to do was to change the target framework to access the libraries.
You can do this like so. First of all, right click on your project in Solution Explorer and go down to Properties. At this point, you should see a Project properties page, with a Target framework dropdown box, change this from .NET Framework x.x (Client Profile) to .NET Framework x.x.
Now, you can simply select the References tab in the Properties window, click on Add and add Microsoft.Office.Interop.Excel.