I'm new in VB.NET and I create an object image Dim myimg As Image that contains an image generated with VB.NET and showing in a picture box.
I wanna add this image to HTML like this : Dim value$ = "<img src='myimg'>
"
Where value is used to replace tag in html file to show the picture generated in VB.NET
Is there a way to do this
Borrowing from this question and this article's helper class, you can do something like this:
Imports System.IO
Imports System.Drawing.Imaging
Public Class ImageHelper
' Convert an image to a Base64 string by using a MemoryStream. Save the
' image to the MemoryStream and use Convert.ToBase64String to convert
' the content of that MemoryStream to a Base64 string.
Public Shared Function ImageToBase64String(ByVal image As Image, _
ByVal imageFormat As ImageFormat)
Using memStream As New MemoryStream
image.Save(memStream, imageFormat)
Dim result As String = Convert.ToBase64String(memStream.ToArray())
memStream.Close()
Return result
: End Using
End Function
' Convert a Base64 string back to an image. Fill a MemorySTream based
' on the Base64 string and call the Image.FromStream() methode to
' convert the content of the MemoryStream to an image.
Public Shared Function ImageFromBase64String(ByVal base64 As String)
Using memStream As New MemoryStream(Convert.FromBase64String(base64))
Dim result As Image = Image.FromStream(memStream)
memStream.Close()
Return result
End Using
End Function
End Class
Within a button:
Dim myImage As Image = Image.FromFile("C:\test.png")
Dim base64 As String = ImageHelper.ImageToBase64String(myImage, ImageFormat.Png)
WebBrowser1.DocumentText = "<img alt=""Embedded Image"" src=""data:image/png;base64," & base64 & """ />"
Related
I have few HTML files which has images stored in binary format like below
<img src="data:image/png;base64,iVBORw0KGgoAAAANS............>
Only one image per HTML file
I need to extract image from each file and save them to png or jpg using a .net application
Any help is appreciated
Thanks
Jiju
Try this:
Imports System.Drawing
Module Module1
Sub Main()
Dim ImageText = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAFEUExURf////+Kh/+Gg//j4//BwP97eP9jX/+Bfv+5uP/29v+HhP+Mif+mpP/b2/8tJ/8IAP+/vv9ybv8cFv9zb/+1tP9xbf/7+/8uKP/q6v/6+v9KRf9aVv/h4f/9/f/Q0P9AO/9nY/9wbP/09P/Av/85NP+Niv/w8P+tq/9fW//5+f8pI//W1v/OzP82Mf8xK/82MP+Jhv99ev8aFP8SC/8zLf8TDP83Mv81MP/s7P81L/96d/84M/9CPf/19f86Nf8sJv9va/9BPP87Nv8yLP9XU/8RCv8kHv89OP9dWf+cmv8+Of8bFf8hG/+urP90cf94df9TTv+opv9STf+2tf+Aff9EP//Cwf/Y2P9MR/9GQf9taf8gGv+Tkf+Vk/9UUP8WD//FxP9JRP+kov8mIP8XEf9hXf/V1f+0s//39//e3v/S0v/i4pkRhsgAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAE5SURBVEhL7ZJXU8JAFIWvqKgoHlE0RBGwoChWVKyI2Hvvvdf//+7evcmMZCIzvjr5HrL3nN1vNpkJeXh4ePyFMp9NuUoVlX5/VbXeqAkEArV1QT0XUw+bBqIQ0KimJt4IS9usTxXRojAQMU2TqBVtUWqPxZFQG2F0mJ1d3UjKOQcGenjpRUrHvn6kWQmpeQCDunMyJMowvxszglFbocyYrpyMizKR0YkoxteJEkVWOgeTWpnCtESKIG4rM3yhC7NaSWNOIuUwz0p+IZVFweocLGolhyWJFMQyKyura+sbVuVkU75la1snoh3ssrJnRTcKouzjQEc6xBErx5JcORHlFMYZr0mcq2cYPg6/cCEKXeIqcX1zi7t7FUorD5ZCj0/qr3p+eeX5raRC79ZK9PH5ZU0/Sg+PfwXRN1h4IRCn6GVqAAAAAElFTkSuQmCC"
'We don't need 'data:image/png;base64,'
ImageText = ImageText.Split(",")(1)
Dim bitmapData As Byte() = Convert.FromBase64String(FixBase64ForImage(ImageText))
Dim streamBitmap As System.IO.MemoryStream = New System.IO.MemoryStream(bitmapData)
Dim bitImage As Bitmap = New Bitmap(CType(Image.FromStream(streamBitmap), Bitmap))
bitImage.Save("c:\temp\test1.png", Imaging.ImageFormat.Png)
End Sub
Public Function FixBase64ForImage(ByVal Image As String) As String
Dim sbText As System.Text.StringBuilder = New System.Text.StringBuilder(Image, Image.Length)
sbText.Replace(vbCrLf, String.Empty)
sbText.Replace(" ", String.Empty)
Return sbText.ToString()
End Function
End Module
I want to add this HTML code into VBA but the all text is in RED highlighted format how to make this valid in VBA window any help will be appreciated.
Is there anyway to create automaticaly joint in Excel vba to use this code into VBA.
ThisHString = "[HTML CODE](https://docs.google.com/document/d/13Hr0aird5XjomeFwL0jz5P5Dm4jWq_w72hPpVh1nEys/edit)
Trying to enabling the HTML text as String in VBA. The code is too long how to add underscores at the last of every row to make joint for the next line.
Here's an example where the HTML is stored in a file "Test.html" in a "Templates" folder located in the same location as the workbook where your code is running.
Public Function CreateHtml(ProductName As String, Condition As String, _
Description1 As String, Description2 As String) As String
Dim TemplateString As String
Dim HtmlValuesRecord As HtmlValues
Dim defs As String
TemplateString = GetContent(thisworkbook.path & "\Templates\Test.html")
HtmlValuesRecord = GetHtmlValues(Category, defs, Conditionone)
CreateHtml = TemplateString
CreateHtml = Replace(CreateHtml, "{product-content}", ProductName)
CreateHtml = Replace(CreateHtml, "{Like New}", Condition)
CreateHtml = Replace(CreateHtml, "{grade-description}", HtmlValuesRecord.Description1)
CreateHtml = Replace(CreateHtml, "{Product description}", HtmlValuesRecord.Description2)
End Function
'read all content from a file
Function GetContent(f As String) As String
GetContent = CreateObject("scripting.filesystemobject"). _
opentextfile(f, 1).readall()
End Function
In my web app i need to convert images on catalog to string
Catalogue are created in a listview with .NET Framework 4 using webform
Protected Sub ProductsLv_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ProductsLv.ItemDataBound
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim dataRow = DirectCast(e.Item.DataItem, DataRowView)
Dim path = Replace("~/Products/Immagine.ashx?FileName=" & dataRow("ImageName"), "\\machine\Foto\", "")
path = "http://" & Me.Request.Url.Host & ResolveUrl(path)
Dim sBase64 As String = "data:image/jpeg;base64,"
Using w As New System.Net.WebClient()
Dim buffer As Byte() = w.DownloadData(path)
sBase64 &= Convert.ToBase64String(buffer)
End Using
DirectCast(e.Item.FindControl("myIMG"), System.Web.UI.WebControls.Image).ImageUrl = sBase64
End If
End Sub
Code above seems works well because i found string on image source of html produced by server response
but images are not visible
What mistake i making?
The value of sBase64 is
data:image/jpeg;base64,DQo8IURPQ1RZUEUgaHRtbCBQVUJMSUMgIi0vL1czQy8vRFREIFhIVE1MIDEuMCBUcmFuc2l0aW9uYWwvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvVFIveGh0bWwxL0RURC94aHRtbDEtdHJhbnNpdGlvbmFsLmR0ZCI+DQoNCjxodG1sIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hodG1sIj4NCjxoZWFkPjx0aXRsZT4NCglMb2dpbg0KPC90aXRsZT4NCiAgICA8c3R5bGUgdHlwZT0idGV4dC9jc3MiPg0KICAgICAgIC5mYWlsdXJlTm90aWZpY2F0aW9uDQp7DQogICAgZm9udC1zaXplOiAxLjJlbTsNCiAgICBjb2xvcjogI0ZGMDA2NjsNCiAgICBmb250LXdlaWdodDpib2xkOw0KfQ0KICAgIDwvc3R5bGU+DQoNCiAgICA8bGluayBocmVmPSIuLi9Db250ZW50L2Jvb3RzdHJhcC5jc3MiIHJlbD0ic3R5bGVzaGVldCIgLz48bGluayBocmVmPSIuLi9Db250ZW50L2Jvb3RzdHJhcC5taW4uY3NzIiByZWw9InN0eWxlc2hlZXQiIC8+PC9oZWFkPg0KPGJvZHk+DQogICAgPGZvcm0gbWV0aG9kPSJwb3N0IiBhY3Rpb249Ii4vTG9naW4uYXNweD9SZXR1cm5Vcmw9JTJmYmVwc2l0ZSUyZkFydGljb2xpJTJmSW1tYWdpbmUuYXNoeCUzZkZpbGVOYW1lJTNkVFNTVDE5NzFTdGFtcGEuanBnJmFtcDtGaWxlTmFtZT1UU1NUMTk3MVN0YW1wYS5qcGciIGlkPSJmb3JtMSIgY2xhc3M9ImZvcm0tc2lnbmluIj4NCjxpbnB1dCB0eXBlPSJoaWRkZW4iIG5hbWU9Il9f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ZRNjEvVXc3c05VWWxPZU8va2F3Y245ZlgwQnBXOFR5NjJvM3hqK3pSMEhHQ1kxVG1NZnNDU1NVWGJONThCaDY3MXROaUF4Zz0iIC8+DQogICAgICA8ZGl2IGNsYXNzPSJjb250YWluZXIiIHN0eWxlPSJ3aWR0aDogNDAwcHgiPg0KICAgICA8aDIgc3R5bGU9InRleHQtYWxpZ246IGNlbnRlciIgPg0KICAgICAgICBBY2NlZGkNCiAgICA8L2gyPg0KICAgIDxwPg0KICAgICAgIA0KICAgICAgDQoNCiAgICAgICAgICANCiAgIA0KICAgDQogICAgICAgICAgICANCiAgICAgICANCiAgICANCiAgICAgICAgICAgIDxzcGFuIGNsYXNzPSJmYWlsdXJlTm90aWZpY2F0aW9uIj4NCiAgICAgICAgICAgICAgICANCiAgICAgICAgICAgIDwvc3Bhbj4NCiAgICAgICAgICAgIA0KICAgICAgICAgICAgPGRpdiBjbGFzcz0iYWNjb3VudEluZm8iPg0KICAgICAgICAgICAgICAgIDxmaWVsZHNldCBjbGFzcz0ibG9naW4iPg0KICAgICAgICAgICAgICAgICAgICA8bGVnZW5kPkFjY291bnQ8L2xlZ2VuZD4NCiAgICAgICAgICAgICAgICAgICAgPHA+DQogICAgICAgICAgICAgICAgICAgICAgICANCiAgICAgICAgICAgICAgICAgICAgICAgIDxpbnB1dCBuYW1lPSJMb2dpblVzZXIkVXNlck5hbWUiIHR5cGU9InRleHQiIGlkPSJMb2dpblVzZXJfVXNlck5hbWUiIGNsYXNzPSJmb3JtLWNvbnRyb2wiIHBsYWNlaG9sZGVyPSJ1dGVudGUiIC8+DQogICAgICAgICAgICAgICAgICAgICAgICAmbmJzcDsNCiAgICAgICAgICAgICAgICAgICAgPC9wPg0KICAgICAgICAgICAgICAgICAgICA8cD4NCiAgICAgICAgICAgICAgICAgICAgICAgIA0KICAgICAgICAgICAgICAgICAgICAgICAgPGlucHV0IG5hbWU9IkxvZ2luVXNlciRQYXNzd29yZCIgdHlwZT0icGFzc3dvcmQiIGlkPSJMb2dpblVzZXJfUGFzc3dvcmQiIGNsYXNzPSJmb3JtLWNvbnRyb2wiIHBsYWNlaG9sZGVyPSJwYXNzd29yZCIgLz4NCiAgICAgICAgICAgICAgICAgICAgICAgICZuYnNwOw0KICAgICAgICAgICAgICAgICAgICA8L3A+DQogICAgICAgICAgICAgICAgICAgDQogICAgICAgICAgICAgICAgPC9maWVsZHNldD4NCiAgICAgICAgICAgICAgICA8cCBjbGFzcz0ic3VibWl0QnV0dG9uIj4NCiAgICAgICAgICAgICAgICAgICAgPGlucHV0IHR5cGU9InN1Ym1pdCIgbmFtZT0iTG9naW5Vc2VyJExvZ2luQnV0dG9uIiB2YWx1ZT0iQWNjZWRpIiBvbmNsaWNrPSJqYXZhc2NyaXB0OldlYkZvcm1fRG9Qb3N0QmFja1dpdGhPcHRpb25zKG5ldyBXZWJGb3JtX1Bvc3RCYWNrT3B0aW9ucygmcXVvdDtMb2dpblVzZXIkTG9naW5CdXR0b24mcXVvdDssICZxdW90OyZxdW90OywgdHJ1ZSwgJnF1b3Q7TG9naW5Vc2VyVmFsaWRhdGlvbkdyb3VwJnF1b3Q7LCAmcXVvdDsmcXVvdDssIGZhbHNlLCBmYWxzZSkpIiBpZD0iTG9naW5Vc2VyX0xvZ2luQnV0dG9uIiBjbGFzcz0iYnRuIGJ0bi1sZyBidG4tcHJpbWFyeSBidG4tYmxvY2siIC8+DQogICAgICAgICAgICAgICAgPC9wPg0KICAgICAgICAgICAgPC9kaXY+DQogICAgICAgIA0KICAgICANCiAgICA8L2Rpdj4NCiAgICA8L2Zvcm0+DQo8L2JvZHk+DQo8L2h0bWw+DQo=
ImageUrl needs a relative or absolute path. So try setting “src” attribute instead, as follows.
DirectCast(e.Item.FindControl("myIMG"), System.Web.UI.WebControls.Image).Attributes("src") = sBase64
Flow:
1. (OK) i download a json
2. (OK) i parse a value from the json object that contains html
3. (NOT OK) i display the values inside div.countries
my code:
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://example.com/countries.json")
Dim values As JObject = JObject.Parse(result)
Dim finalHTML As String = values.GetValue("countries_html")
basically finalHTML variable looks like this:
<div class="country_name">USA</div>
<div class="country_name">Ireland</div>
<div class="country_name">Australia</div>
Im stuck and dont know how to move on.
I need to go over all div.country_name and get the inner_text of it. Hope that make sense.
Since the finalHTML string already contain only the target div elements, you can simply load the string to HtmlDocument object and use a bit of LINQ to project the divs into collection -IEnumerable, List<T>, or whatever most suitable to your need- of InnerText strings :
....
Dim finalHTML As String = values.GetValue("countries_html")
Dim doc = New HtmlDocument()
doc.LoadHtml(finalHTML)
Dim countries = doc.DocumentNode.Elements("div").Select(Function(o) o.InnerText.Trim())
'print the result as comma separated text to console:
Console.WriteLine(String.Join(",", countries))
Dotnetfiddle Demo
output :
USA,Ireland,Australia
here's a nice article on using the HAP: http://www.mikesdotnetting.com/article/273/using-the-htmlagilitypack-to-parse-html-in-asp-net.
We are in the process of implementing code to read/create 2D bar codes that are starting to show up on our supplier's parts.
We have a need to create the 2D bar codes in MS Access reports and forms. Has anyone had success with the font (IDAutomation) or Active X (dlSoft) solutions out there.
For C#, the open source library "http://barcoderender.codeplex.com/" was suggested. Any thoughts on how successful this was or if anyone has other open-source and/or pay for options.
Thanks,
Anton
Depending on the volume of codes you need to generate, you could use the Google Charts API to generate QR Codes.
Simply add a "Microsoft Web Browser" ActiveX component and the following code to your Form:
Dim Size As Integer
Dim Text As String
Dim URL As String
Size = 200
Text = "This is my test"
' Better to actually use a URL encoding function like those described here:
' http://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba
Text = Replace(Text, " ", "%20")
URL = "http://chart.apis.google.com/chart?chs=" & Size & "x" & Size & "&cht=qr&chld=H|0&chl=" & Text
WebBrowser.Navigate (URL)
You can of course change the Size and the Text depending on your need. The Text can also be a value directly from your Form, therefore your data.
I would advise you to check Googles Terms and Services before using it.
I completely escaped the web browser control and the Google API since that functionality is now deprecated from what I can tell. I went with a different free API but the Google API or any other API could be used instead.
In my example I am creating an .png image in the same directory as the application. I have a text box on my form named txtToCode in which I type in any text I want to code. I also have an image control so that the image can be viewed from the form, but you can modify it how you wish:
Private Sub btnCode2_Click()
Call GetQRCode(Me.txtToCode, 150, 150)
End Sub
Sub GetQRCode(Content As String, Width As Integer, Height As Integer)
Dim ByteData() As Byte
Dim XmlHttp As Object
Dim HttpReq As String
Dim ReturnContent As String
Dim EncContent As String
Dim QRImage As String
EncContent = EncodeURL(Content)
HttpReq = "https://api.qrserver.com/v1/create-qr-code/?data=" & EncContent & "&size=" & Width & "x" & Height & ""
Set XmlHttp = CreateObject("MSXML2.XmlHttp")
XmlHttp.Open "GET", HttpReq, False
XmlHttp.Send
ByteData = XmlHttp.responseBody
Set XmlHttp = Nothing
ReturnContent = StrConv(ByteData, vbUnicode)
Call ExportImage(ReturnContent)
End Sub
Private Sub ExportImage(image As String)
Dim FilePath As String
On Error GoTo NoSave
' Build Export Path
FilePath = Application.CurrentProject.Path & "\qr.png"
Open FilePath For Binary As #1
Put #1, 1, image
Close #1
Me.Image3.Picture = FilePath
' Save File Path
Exit Sub
NoSave:
MsgBox "Could not save the QR Code Image! Reason: " & Err.Description, vbCritical, "File Save Error"
End Sub
Private Function EncodeURL(str As String)
Dim ScriptEngine As Object
Dim encoded As String
Dim Temp As String
Temp = Replace(str, " ", "%20")
Temp = Replace(Temp, "#", "%23")
EncodeURL = Temp
End Function