Is there a way to set the columnwidth in an Excel document via VB.NET in pixels?
I am using currently the following codeline:
Sheets(i).Columns("A:A").ColumnWidth = 3.14
Another problem is that I used a macro to record the width of the column and inserted it with the above statement in my code. But when I run the code the column width is 3,42 instead of 3,14.
Can anybody help me?
Based on this post, you can perform a calculation to convert the excel unit of measure to pixels.
Here is a snippet from that web site:
COLUMNWIDTH: One unit of column width is equal to the width of one character
in the Normal style.
range("A1").Columnwidth
returns 8.43 characters
WIDTH: Returns or sets an object's width, in points.
range("A1").width
returns 48 points. 72 point/inch=.6666"=64 pixels # 96 pixels/inch
So...
Sub testwidth()
Sheets("sheet3").Activate
screenres = 96 '96/inch
mypoints = Sheets("sheet3").Range("A1").Width
'> returns 48 points
mychars = Sheets("sheet3").Range("A1").ColumnWidth
'> returns 8.43 chars
mypixels = (mypoints / 72) * screenres 'pixel width of column
Debug.Print mypoints, mychars, mypixels
'> returns 48 8.43 64
End Sub
The column width is 48 points or 8.43 characters or 64 pixels.
Excel doesn't use the concept of pixels when it comes to width of the columns.
One unit of column width is equal to the width of one character in the Normal style as documented on MSDN here: [https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.tools.excel.namedrange.columnwidth?redirectedfrom=MSDN&view=vsto-2017#Microsoft_Office_Tools_Excel_NamedRange_ColumnWidth][1]
Hope this helped.
The other way to solve this issue is to convert pixels to above mentioned character and set it.
Related
What is the default measurement unit of Excel cell size? Is it Point or Pixel or Millimeter ?
By default, excel cell Row height is 15, what is the meaning of this value? Is it 15 Pixels or 15 Points
By default, excel cell Column Width is 8.43, what is the meaning of this value? Is it 8.43 Pixels or 8.43 Points
If both row and column units are same, then Row height should be smaller than to column width. But the measurement is reverse, row height shows bigger number than column width. In Cell appearance also, row is small than column width.
I need to create box with Height 90 mm (millimeter) and Width 195 mm (millimeter). Please let me know what are values to be put in Row and Column textboxes.
Thanks in advance.
The default units for column and row are indeed different when accessed through the GUI.
The displayed column width in the GUI refers to the Range.ColumnWidth property, where One unit of column width is equal to the width of one character in the Normal style. For proportional fonts, the width of the character 0 (zero) is used (source). This means as you change the worksheet style, your column width may change too.
The height, however, displays a normal height in points.
In VBA, you can both get both this font-related unit, and the normal point unit for the width. For the height, you can only get the value in points:
Debug.Print Range("A1").ColumnWidth '8.43 characters wide by default
Debug.Print Range("A1").Width '48 points wide by default
Debug.Print Range("A1").Height '12.75 points high by default
Of course, you can calculate a conversion factor between character width and points: Range("A1").Width / Range("A1").ColumnWidth = 5.69 when using Arial, 10 pt. This means that if you want to have a size of 195mm by 90mm, you need to enter 97.0777 as column width, and 255.118 as column height if you're using Arial, 10 pt as normal style.
As per the Microsoft documentation....
You can specify a row height of 0 (zero) to 409. This value represents the height measurement in points (1 point equals approximately 1/72 inch or 0.035 cm). The default row height is 12.75 points (approximately 1/6 inch or 0.4 cm). If a row has a height of 0 (zero), the row is hidden.
Read it more here.
Please throw this link: Microsoft reference
You can change and define this unit manually where pointed in above link:
On the File tab, click Options, click the Advanced category, and under Display, select an option from the Ruler Units list.
So please visit Here Microsoft reference that exactly explain
what you asked.
Above you asked are the pixel width retranslated into character units (based on the Normal font) for display.
I am trying to use the Height function in VBA but I am getting strange numbers. Instead of returning the height of a range, it is returning the height of a range multiplied by 15. For example, the following simple function :
Function h(c1 As Range) As Double
h = c1.Height
End Function
would give a value of 15 if c1 was a single cell, would give 30 if c1 was two cells high, and so on. I should clarify that I am using this as part of a larger function, but it is this issue that is causing the problem. I wanted to make a function as simple as possible to see if the problem remained, and it has. I really don't understand why this is happening, I have used the height function before with no problem. Any advice would be greatly appreciated,
Thanks
You are misunderstanding what the height property is. The height is its height in points...
From here..https://msdn.microsoft.com/en-us/library/office/aa215509%28v=office.11%29.aspx?f=255&MSPPError=-2147217396
Returns the height of all the rows in the range specified, measured in points. Returns Null if the rows in the specified range aren't all the same height. Read/write Variant.
Remarks
You can use the Height property to return the total height of a range of cells.
Differences between RowHeight and Height include the following:
Height is read-only.
If you return the RowHeight property of several rows, you will either get the row height of each of the rows (if all the rows are the same height) or Null (if they're different heights). If you return the Height property of several rows, you will get the total height of all the rows.
Example
This example doubles the height of row one on Sheet1.
With Worksheets("Sheet1").Rows(1)
.RowHeight = .RowHeight * 2
End With
One of the part of my project is RichTextBox with string length form 10 to 50 chars, and font's size 175-225 in single line. I use special printer: 100 mm height, and 3000 mm (3m) width ribbon. Very long width is a real problem.
I've got problem with Printing.PaperSize width element (I have to use it). It's (MSDN) "The width of the paper, in hundredths of an inch".
I tried to get this from:
RichTextBox1.PreferredSize.Width
g.MeasureString(RichTextBox1.Text, RichTextBox1.Font).Width
g.MeasureCharacterRanges(Text, Font, Rect, Format)
All of them gives me "pixels" but I have no idea how can I use it with Printing.PaperSize - all of them are too short, and dependent on used font family.
Tricky part of this is that I need very precise length of my PaperSize, because I have to print several items before and after the string.
Is any way to estimate printed width (in cm/inches) when I have only pixel size of element?
If anyone can give me any answer in C#, or C++ it doesn't matter - I will be grateful.
You can measure the width of the string in a given font:
Sub ShowWidthOfText(s As String)
Using img As New Bitmap(1, 1)
img.SetResolution(300, 300)
Using f As New Font("Times New Roman", 225, GraphicsUnit.Point)
Using g = Graphics.FromImage(img)
g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
Dim w = g.MeasureString(s, f)
MessageBox.Show(String.Format("{0} pixels = {1:N2} cm at {2} ppi", w.Width, w.Width * 2.54 / g.DpiX, g.DpiX))
End Using
End Using
End Using
End Sub
which gives 90.75 cm (35.73 in) for "lorem ipsum dolor sit amet" in the font you suggest, which seems reasonable to me. If you are intending to set the the width of the paper size from that, you would of course need a minimum of 3573 hundredths of an inch.
If you want to fit the text to a particular width, it would probably be best to use a successive approximation method rather than a direct calculation because the kerning of the text may depend on the size of the font.
The .SetResolution is in there in case you use that code to construct your output, it makes no difference to the measurement.
(The resolution names in .NET are wrong: they should be, e.g. PpiX rather than DpiX etc.)
Ref: Measure a String without using a Graphics object?
i've got to this stage:
where i can find the numbers in the above image but i need to cut them out so i can retain the order etc. but the as the number increases the spacing changes and the position of the number?
so i think it should be a find a white PX the continue until it find a solid black col and then use the points to do a simple cut any help would be great.
A simple solution would be this:
Find the first upmost horizontal line which contains white pixels
From that line find the first horizontal line which contains only black pixels
Those two lines are your upper and lower borders.
Between this borders proceed like this:
Find the first most left vertical line which contains white pixels
From that line find the last vertical line which contains only black pixels and which comes directly after a line with white pixels.
Those two lines are your left and right borders.
The steps to separate single numbers can be performed analogously.
If you need to identify which numbers are in your picture, I recommend using specialized computer vision libraries.
Some VB.net pseudo code to get you going:
Sub FindTopBorder(image As MyImage) As Integer
For y = 0 to image.Height - 1
For x = 0 to image.Width - 1
Dim pixel = image.GetPixel(x, y)
If ('Check if pixel is white here with RGB or Color') Then
Return y
End If
Next
Next
' Just in case there are no white pixels or use an exception instead
Return -1
End Sub
I would start looking into Connected component segmentation. You find a pixel which is within a character (number). Then run the connected component algorithm which finds all connected pixels under specific set of rules (e.g. slight deviation in color, stop at hard borders etc).
http://en.wikipedia.org/wiki/Connected-component_labeling
If you can use libraries, I'm sure OpenCV or similar libraries support this out of the box.
//edit
I see you need VB.net. Probably it is easiest to port some algorithm to VB or create one yourself.
See e.g. http://www.codeproject.com/Articles/336915/Connected-Component-Labeling-Algorithm
What to expect
Input
An image containing two shapes:
Output
Now each is separated into single images.
I want to print bill on a roll paper using vb.net.
The requirements are as follows:
The width of the page is 300 pixels or 3 inches.
The height of the page is variable, depending on the number of the rows in the datagrid.
The page header will have an image.
The names of the items could be long, so they should not be chopped, rather print on the next line.
How should I go about it?
Get your Image height and xRows (dgv.rows.count) * Rowheight
So you have an Dynamic height.
Additional you could count string lengtht of your long Values and add for longer datarows the addidtional height
But that is just my idea.
Im sure there are better solutions