I am trying to generate a barcode with its text embedded under the very barcode itself but I can only generate the barcode without the text embedded on it.
Here is my code :
Public Function process_printbarcode(lbl169 As Label)
Dim length As Integer = 1
Dim mybarcode As Image = Code128Rendering.MakeBarcodeImage(lbl169.Text.ToString, Integer.Parse(length.ToString()), False)
Admin_Menu.PictureBox3.Image = mybarcode
Return True
End Function
GenCode128 does not include an option to display the encoded value below the barcode as part of the image. You could use the Bitmap and Graphics classes to modify the image and add text to it, but I think it would be easier to just use a different DLL that comes with that functionality. One I've personally used is BarcodeLib. You can add it to your project in a few ways:
Add it as a Nuget Package by running Install-Package BarcodeLib -Version 1.0.0.23 in your package manager console
Download the project's GitHub source-code and build the BarcodeLib.dll yourself
Either way, just add it as a reference in your solution. BarcodeLib has got a lot more barcode parameters available for you to set (and several other encoding types as well), and it's very easy to create them:
Private Function Code128Image(ByVal value As String, _
Optional height As Integer = 100, _
Optional barWidth As Integer = 1, _
Optional labelIncluded As Boolean = True, _
Optional labelPosition As BarcodeLib.LabelPositions = LabelPositions.BOTTOMCENTER, _
Optional barcodeRotation As System.Drawing.RotateFlipType = System.Drawing.RotateFlipType.RotateNoneFlipNone) _
As System.Drawing.Image
Using barcodeImage As New BarcodeLib.Barcode
With barcodeImage
.Height = height
.BarWidth = barWidth
.IncludeLabel = labelIncluded
.LabelPosition = labelPosition
.RotateFlipType = barcodeRotation
Return .Encode(BarcodeLib.TYPE.CODE128, value)
End With
End Using
End Function
Calling Admin_Menu.PictureBox3.Image = Code128Image("123456789") would get you:
Related
I'm working on a windows forms application, using vb.net and microsoft sql server as backend. As for the reports, I'm using microsoft's rdlc, which has been quite satisfactory for me until I was struck with this distinct problem.
So, in the report I'm using external images, which are loaded through local filepaths, passed as parameters. I retrieve these paths from the database and pass it to the report. This method worked for me for a long time, until it stopped working. Now the case is, that the external images are not loading in the report (a red cross is displayed instead of the image). As far as I feel, this problem has something to do with permissions on my PC because the same code and configurations worked for me before, but I'm not able to resolve it. I have searched everywhere to no end and would definitely want assistance.
My code for the report is:
Try
With Me.ReportViewer1.LocalReport
.DataSources.Clear()
.ReportPath = Application.StartupPath & "\RptArticle.rdlc"
.EnableExternalImages = True
Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics", imagepath)
Dim imgparameter As New ReportParameter
imgparameter = New ReportParameter("ImagePath", "file://" & imagepathstring, True)
.SetParameters(imgparameter)
Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode", barpath)
Dim barcode As New ReportParameter("Barcode", "file://" & barpathstring, True)
.SetParameters(barcode)
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleAccessory", Accdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleSize", AccSdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleColour", AccCdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticleColour", AccCdt))
.DataSources.Add(New Microsoft.Reporting.WinForms.ReportDataSource("DSArticle", dt))
End With
Me.ReportViewer1.ZoomMode = ZoomMode.PageWidth
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Me.ReportViewer1.RefreshReport()
When I load the report, the output is:
Warning: Images with external URL references will not display if the report is published to a report server without an UnattendedExecutionAccount or the target image(s) are not enabled for anonymous access. (rsWarningFetchingExternalImages)
Warning: The value of the MIMEType property for the image ‘Image1’ is “application/octet-stream”, which is not a valid MIMEType. (rsInvalidMIMEType)
Warning: The value of the ImageData property for the image ‘Image1’ is “”, which is not a valid ImageData. (rsInvalidExternalImageProperty)
Warning: The value of the MIMEType property for the image ‘Image2’ is “application/octet-stream”, which is not a valid MIMEType. (rsInvalidMIMEType)
Warning: The value of the ImageData property for the image ‘Image2’ is “”, which is not a valid ImageData. (rsInvalidExternalImageProperty)
The thread 0x3948 has exited with code 0 (0x0).
Instead of using external images, you can pass your images as string to your report as parameters.
Convert your image to Base64 string first using this:
Public Function ImageToBase64(ByVal image As Image, ByVal format As System.Drawing.Imaging.ImageFormat) As String
Dim base64String As String = ""
Using ms As New System.IO.MemoryStream()
image.Save(ms, format)
Dim imageBytes As Byte() = ms.ToArray()
base64String = Convert.ToBase64String(imageBytes)
End Using
Return base64String
End Function
So this:
Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics", imagepath)
Dim imgparameter As New ReportParameter
imgparameter = New ReportParameter("ImagePath", "file://" & imagepathstring, True)
.SetParameters(imgparameter)
Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode", barpath)
Dim barcode As New ReportParameter("Barcode", "file://" & barpathstring, True)
.SetParameters(barcode)
will be like this:
Dim imagepathstring As String = System.IO.Path.Combine(folder & "\ArticlePics", imagepath)
Dim imgparameter As New ReportParameter
imgparameter = New ReportParameter("ImagePath", ImageToBase64(Image.FromFile(imagepathstring),<THE IMAGE FORMAT OF YOUR IMAGE>))
.SetParameters(imgparameter)
Dim barpathstring As String = System.IO.Path.Combine(folder & "\BarCode", barpath)
Dim barcode As New ReportParameter("Barcode", ImageToBase64(Image.FromFile(barpathstring),<THE IMAGE FORMAT OF YOUR IMAGE>))
.SetParameters(barcode)
Then, in your report set the following for:
ImagePath
MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>
and in the Expression window:
=System.Convert.FromBase64String(Parameters!ImagePath.Value)
Barcode
MIMEType = select the correct MIME type from the dropdown list
Source = Database
Value = <Expression>
=System.Convert.FromBase64String(Parameters!Barcode.Value)
I am working on a project and had to refer to a already completed project.While going through[enter image description here][1] it I encountered a problem and wasn't able to understand the code
I want to know what is adosetup doing.
Msmt_system is table name
While msmt_sys_desc and msmt_sys_val are columns in msmt_system
dbcBoLC is datacombobox
How does datacombo know from which table to access data?
Call adoSetup(AdoCom, "Msmt_System", True, dbcBoLC, "Msmt_Sys_Desc",
"Msmt_Sys_val")
Call dbcBoLC_Click(1)
'in another file:
Public Sub adoSetup(adoCtrl As Adodc, sRecSrc As String,
bLinkAdoCtrl As Boolean,
Optional dCombo As DataCombo,
Optional sListField As String,
Optional sBoundCol As String)
'Added On: 25/07/2003
sAppPath = App.Path & "\MultiGauging.mdb" 'Store the Application Path
sDbConn_String = "DBQ=" & sAppPath & ";Driver={Microsoft Access Driver (*.mdb)};pwd=*****;UID=admin;UserCommitSync=Yes;"
'Begin: Setup the connection string and apply to datacontrol
With adoCtrl
.ConnectionString = sDbConn_String
.RecordSource = sRecSrc
.Refresh
End With
'End: Setup the connection string and apply to datacontrol
'Begin: Setup the corresponding combo box
If bLinkAdoCtrl Then
Set dCombo.RowSource = adoCtrl
dCombo.ListField = sListField
dCombo.BoundColumn = sBoundCol
If Not (adoCtrl.Recordset.EOF Or adoCtrl.Recordset.BOF) Then
adoCtrl.Recordset.MoveFirst
dCombo.BoundText = adoCtrl.Recordset(sBoundCol)
End If
End If
'End: Setup the corresponding combo box
End Sub
In your call:
Call adoSetup(AdoCom, "Msmt_System", True, dbcBoLC, . . .
The table name is the 2nd argument. That is how it knows which table (recordsource) to use. Your sub adoSetup just establishes a connection (ConnectionString = sDbConn_String) queries a table (RecordSource = sRecSrc) then it attaches the data to a combo box (arg 4 to this sub: dbcBoLC). The datacombo just uses whatever table was set in adoCtrl.
I have the following code but I need to adjust. I want the user to select a particular folder from within a project. Imagine the path "C:\Project\SomeOtherFolder\WINDOW". The below code only fills the text box if the have selected the "WINDOW" folder. I'm just using this as a check for the user, but I actually want the text box to fill with "Project".
Using fb As New FolderBrowserDialog
If fb.ShowDialog = Windows.Forms.DialogResult.OK AndAlso _
(IO.Path.GetFileName(fb.SelectedPath) = "WINDOW") Then
TextBox1.Text = IO.Path.GetFileName(fb.SelectedPath)
Else
Exit Sub
End If
End Using
How can I accomplish this please? Many Thanks!!!
This UDF, should give you what you need. I have created the function to return the name of the folder up from a specific folder location. I have included some optional parameters so you could (if required) change the requirement.
Public Function GetFolderName(FolderPath As String, _
Optional endPath As String = "WINDOW", _
Optional moveUp As Integer = 2) As String
Dim tmpArr() As String, retStr As String
tmpArr = Split(FolderPath, "\")
If InStr(FolderPath, endPath) <> 0 And moveUp <= UBound(tmpArr) Then
retStr = tmpArr(UBound(tmpArr) - moveUp)
End If
GetFolderName = retStr
End Function
So the code walk through. You send in the Path you obtain in the previous step and then you simply call the function as,
TextBox1.Text = GetFolderName(fb.SelectedPath)
'Or - However this is redundant as the Optional Parameters are declared as such by default
TextBox1.Text = GetFolderName(fb.SelectedPath, "WINDOW", 2)
The above would populate your text box as "Project". Hope this helps !
Hi Guys, I have searched for different methods for this thing but couldn't get it right. Used AutoEllipses Property, Set the MaximumSize too but to no avail. How can i get a label to show the name of the file being scanned like in the pic i attached? I mean, the label should show some part from the beginning of full path of the file then some dots and then the file name with extension.
You might consider a few things; however, the range of possibilities is too large to cover, here.
There are three (3) things you need to know in order to code this properly: the actual measured size of the filepath string you are sending to the label; the measured size of the label; and the length (in characters) of your file name. There may be a fancy function that reduces the number of things you need to do and know; however, I am not about to read oodles of documentation.
All of the above things need to be dynamic so that your label can take different String objects and render them, properly.
Dim filePath As String = ""
Dim FileDirectory As String = ""
Dim fileName As String = ""
Dim filePathLength As SizeF = 0.0
Dim labelLength As Double = 0.0
Dim fileNameLength As Integer = 0.0
' Come up with a way for measuring your string:
Dim _GraphicsUnit As Graphics = Me.CreateGraphics()
' Receive your file path, here:
' and work with your file path-related Strings:
filePath = ' SOMETHING
fileDirectory = Path.GetDirectoryName(filePath)
fileName = Path.GetFileName(filePath)
fileNameLength = fileName.Length()
' Measure the length of you path
filePathLength = _GraphicsUnit.MeasureString(filePath, INSERTFONT) * _GraphicsUnit.Inches 'or other usable unit
If filePathLength > SIZEOFLABEL Then
While filePathLength > SIZEOFLABEL
' Grab a substring of the the fileDirecory, append the "...", and keep measuring until shorter
' than SIZEOFLABEL.
' Your algorithm will need to figure out how and when to re-append the fileName
End While
End If
The above is pseudo-code and is rife with errors. The above is means to demonstrate some of the tools .Net can provide you, here namely the GraphicsUnit stuff and the Path. stuff. Both of those are helpful. You will essentially be juggling those two 'things' and the SubString() method.
My attempt is to show you how to begin to think about the problem you have in front of you so that you can begin to tackle the problem (because as the comments above state, there isn't much out there that will do what you need). Your initial question does not provide any original code on which to base the above pseudo-code; in other words, I don't feel like coding your whole project but at least want to get the answers ball rolling.
An Additional Thought: .MaxLength
The above approach is quite memory intensive - requiring a lot of repetition that may not be be necessary. Simply knowing the size - in this case the MaxLength property - might be helpful. Setting the .MaxLength property of the TextBox will allow you to know how many characters can fit in the box (you'd need to consider a few other elements, e.g. font, size, etc.).
Knowing this number, you could avoid looping altogether:
SubString of fileDirectory equal to the length of .MaxLength property, remove number of characters equating to size of fileName and "..." and append the latter two.
I got an answer to this problem here Shorten The File Path and it's a very short solution as far as code is concerned.
You can use the PathCompactPathExW pInvoke method to accomplish this:
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Program
<DllImport("shlwapi.dll", EntryPoint:="PathCompactPathExW", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Public Shared Function PathCompactPathEx(<MarshalAs(UnmanagedType.LPTStr)> pszOut As System.Text.StringBuilder, _
<MarshalAs(UnmanagedType.LPTStr)> pszSrc As String, _
cchMax As UInteger, _
reserved As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public Shared Sub Main()
Dim longPath As String = "c:\a\very\very\long\path\that\needs\to\be\shortened\by\calling\the\PathCompactpathEx.ext"
Dim length As Integer = 40
Dim result As String = CompactPath(longPath, length)
'Prints c:\a\very\very\...\PathCompactpathEx.ext
Console.WriteLine(result)
Console.ReadLine()
End Sub
Public Shared Function CompactPath(longPathName As String, wantedLength As Integer) As String
'NOTE: You need to create the builder with the required capacity before calling function.
'See http://msdn.microsoft.com/en-us/library/aa446536.aspx
Dim sb As New StringBuilder(wantedLength + 1)
PathCompactPathEx(sb, longPathName, CUInt(wantedLength + 1), 0)
Return sb.ToString()
End Function
End Class
I am trying to make a JQGrid for a simple table.
After following through with a VB translated version from
http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx
from
http://www.qa.downappz.com/questions/jqgrid-sorting-in-vb-net-mvc-app.html
I modified it to my own database and came up with this function
Public Function SelectGridData(ByVal sidx As String, ByVal sord As String, ByVal page As Integer, ByVal rows As Integer) As ActionResult
Dim context As New IssueDBEntities
Dim pageIndex As Integer = Convert.ToInt32(page) - 1
Dim pageSize As Integer = rows
Dim totalRecords As Integer = context.Issues.Count()
Dim totalPages As Integer = CInt(Math.Ceiling(CSng(totalRecords) / CSng(pageSize)))
Dim jsonData = New With { _
.total = totalPages, _
.page = page, _
.records = totalRecords, _
.rows = (From p In context.Issues _
Order By (p.ID & " " & sord) _
Select New With {.id = p.ID, .cell = _
{p.ID, p.Image_Path, p.Magazine_Type,p.Magazine_Path}}).ToArray()}
Return Json(jsonData, JsonRequestBehavior.AllowGet)
End Function
The grid does show up without any data, and the system throws an error
The error description says
"Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types."
Any help is appreciated, if this is due to some basic misunderstanding, please guide me, I am willing to do some hard work.
Thank you
Edit: The code that finally worked as per Oleg's Suggestion
Dim Simple_Object As IQueryable(Of Object)
Dim Second_Simple_Object As IQueryable(Of Object)
Dim My_Array As Array
Dim My_Second_Array As Array
Simple_Object = From p In Context.Issues _
Order By (p.ID & " " & sord) _
Select New With {p.ID, p.Image_Path, p.Magazine_Type, p.Magazine_Path}
My_Array = Simple_Object.ToArray()
Second_Simple_Object = From p In Context.Issues _
Order By (p.ID & " " & sord) _
Select New With {p.ID}
My_Second_Array = Second_Simple_Object.ToArray()
Dim My_Result(0) As My_Record_Type
For i = 0 To My_Array.GetLength(0) - 1
If i > 0 Then
ReDim Preserve My_Result(i)
End If
My_Result(i) = New My_Record_Type
My_Result(i).id = CInt(My_Second_Array(i).ID)
My_Result(i).Cell = {My_Array(i).ID.ToString, My_Array(i).Image_Path.ToString, _
My_Array(i).Magazine_Type.ToString, My_Array(i).Magazine_Path.ToString}
Next
Class My_Record_Type
Public id As Integer
Public Cell As String()
End Class
You try to fill rows property in one step. The problem is that you use Entity Framework which have some restrictions in the conversion of the data types. You can solve the problem if you first make query to get the items which you need without any data conversion and save intermediate results as List of items. After that you can make another LINQ Query where you include explicit conversion of p.ID to string. Moreover your current code don't use any paging of data. So the user will never seen more as the first page if you don't use loadonce: true.
It's difficult for me to write correct code in VB without debugging it. I use C# instead since last years. I recommend you to look at the answer for more information. It contains implementation of the server side filtering. If you don't need it and remove the corresponding code the rest code will be very short and I hope it will be easy to understand.
Here is complete sample of JQgrid with ASP.NET MVC 3 + C# (you can convert it to vb.net)
http://www.dotnetacademy.blogspot.in/2012/04/using-jqgrid-in-aspnet-mvc-3.html