I'm failing at signaling my printer to start a new page. It continues to print on page 1 after I set e.HasMorePages = True. With my font, I can cleanly print ~ 30 lines per page and still maintain the border
'print the text
'create a string array from the listView equipment items
Dim equip() As classReportObj
ReDim equip(0 To lstReport.Items.Count - 1)
For i = 0 To lstReport.Items.Count - 1
equip(i) =
New classReportObj() With {.name = lstReport.Items(i).Text, .type = lstReport.Items(i).SubItems(1).Text,
.completeTests = lstReport.Items(i).SubItems(2).Text, .incompleteTests = lstReport.Items(i).SubItems(3).Text,
.status = lstReport.Items(i).SubItems(4).Text}
Next i
'get the coordinates for the first row and the columns
Dim y As Integer = e.MarginBounds.Top
Dim x0 As Integer = e.MarginBounds.Left
Dim x1 As Integer = x0 + 150
Dim x2 As Integer = x1 + 150
Dim x3 As Integer = x2 + 150
Dim x4 As Integer = x3 + 120
Dim headerFont As New Font("Times New Roman", 10)
Dim maxLines As Integer 'maximum number of lines per page
maxLines = 30 '30 lines per page, including headers
Dim lineCount As Integer 'counts lines per printed page
'make a new font to use
Using theFont As New Font("Times New Roman", 10)
'draw the column headers
e.Graphics.DrawString("Project: " & project.name & " " & thisReportType & " Report, " & Now, theFont, Brushes.Black, x0, y)
e.Graphics.DrawString("Name", headerFont, Brushes.Black, x0, y + 30)
e.Graphics.DrawString("Type", headerFont, Brushes.Black, x1, y + 30)
e.Graphics.DrawString("Complete Tests", headerFont, Brushes.Black, x2, y + 30)
e.Graphics.DrawString("Incomplete Tests", headerFont, Brushes.Black, x3, y + 30)
e.Graphics.DrawString("Status", headerFont, Brushes.Black, x4, y + 30)
'mmove Y down for the next row
y += 60
Dim nameMax As Integer 'max characters for name
Dim typeMax As Integer 'max characters for type
'loop through each equipment to display the data
For Each aEquip In equip
'set the max character length for name and type
If aEquip.name.Length < 23 Then
nameMax = aEquip.name.Length
Else
nameMax = 23
End If
'
If aEquip.type.Length < 23 Then
typeMax = aEquip.type.Length
Else
typeMax = 23
End If
'display the equipment values
e.Graphics.DrawString(aEquip.name.Substring(0, nameMax), theFont, Brushes.Black, x0, y)
e.Graphics.DrawString(aEquip.type.Substring(0, typeMax), theFont, Brushes.Black, x1, y)
e.Graphics.DrawString(aEquip.completeTests, theFont, Brushes.Black, x2, y)
e.Graphics.DrawString(aEquip.incompleteTests, theFont, Brushes.Black, x3, y)
e.Graphics.DrawString(aEquip.status, theFont, Brushes.Black, x4, y)
'move Y down for the next row
y += 30
'increment the line counter for each piece of equipment
lineCount = lineCount + 1
'if we've reached the maximum number of lines per page
If (lineCount Mod maxLines) = 0 Then
'draw a box around it all
e.Graphics.DrawRectangle(Pens.Black, x0, e.MarginBounds.Top + 30, x4 - x0 + 100, y - e.MarginBounds.Top - 30)
e.HasMorePages = True 'But it doesn't start a new page, it continues printing page 1 unless I exit
Else
e.HasMorePages = False
End If
Next
End Using
'draw a box around it all
e.Graphics.DrawRectangle(Pens.Black, x0, e.MarginBounds.Top + 30, x4 - x0 + 100, y - e.MarginBounds.Top - 30)
'only printing one page (for now)
e.HasMorePages = False
End Sub
You seem not to understand what e.HasMorePages actually does. All it does is cause the PrintPage event to be raised again. If all your PrintPage event handler does is print the first page then that's all you see printed over and over.
The PrintPage event handler is supposed to print one page. The very first thing you would usually do in that event handler is determine what page needs to be printed. That is generally going to require that you keep some sort of count OUTSIDE the event handler.
That count might be specifically how many pages have been printed or it might be an index into a list of records or something else. Whatever it is, it needs to tell you insider the PrintPage event handler how to get what needs to be printed on the current page.
Here's an example that will print a list of Strings, five to a page:
Private lines As New List(Of String)
Private overallRecordIndex As Integer
Private pageNumber As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
overallRecordIndex = 0
pageNumber = 1
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim pageRecordIndex = 0
Dim yOffset = 10
Do While pageRecordIndex < 5 AndAlso overallRecordIndex < lines.Count
e.Graphics.DrawString(String.Format("Page {0}, Record {1}: {2}",
pageNumber,
overallRecordIndex,
lines(overallRecordIndex)),
Me.Font,
Brushes.Black,
10,
yOffset)
overallRecordIndex += 1
pageRecordIndex += 1
yOffset += 20
Loop
e.HasMorePages = (overallRecordIndex < lines.Count)
If e.HasMorePages Then
pageNumber += 1
End If
End Sub
Related
I have a program that take an image and changes it to 1 bit B&W. It uses Lockbyte software to make it faster. BUT, when running it, it takes over a minute to process one image. When looking at the CPU usage it is only 5% at most once it is running. Is there a way to get the computer to use more CPU time? The indicator is showing that the computer is running below 50%, as low as 25%.
I just had one DUH thought, I forgot to add the resizing function into my program. It should help but I know I need to make the conversion faster yet. The program will be used to do 100 to 300 images per batch.
Most other programs I have seen do a conversion within a few seconds per image. I would like to get to something like this too.
This is the program. Mostly cobbled together from samples. I only half understand it but can read it. Sorry to the contributors that I cannot give credit to them. I didn't keep track of them.
Public Class Form1
Public Shared Function ConvertTo1Bit(ByVal input As Bitmap) As Bitmap
Dim masks = New Byte() {&H80, &H40, &H20, &H10, &H8, &H4, &H2, &H1}
Dim output = New Bitmap(input.Width, input.Height, PixelFormat.Format1bppIndexed)
Dim data = New SByte(input.Width - 1, input.Height - 1) {}
Dim inputData = input.LockBits(New Rectangle(0, 0, input.Width, input.Height), ImageLockMode.[ReadOnly], PixelFormat.Format24bppRgb)
Try
Dim scanLine = inputData.Scan0
Dim line = New Byte(inputData.Stride - 1) {}
Dim y = 0
While y < inputData.Height
Marshal.Copy(scanLine, line, 0, line.Length)
For x = 0 To input.Width - 1
data(x, y) = CSByte((64 * (GetGreyLevel(line(x * 3 + 2), line(x * 3 + 1), line(x * 3 + 0)) - 0.5)))
Next
y += 1
scanLine += inputData.Stride
End While
Finally
input.UnlockBits(inputData)
End Try
Dim outputData = output.LockBits(New Rectangle(0, 0, output.Width, output.Height), ImageLockMode.[WriteOnly], PixelFormat.Format1bppIndexed)
Try
Dim scanLine = outputData.Scan0
Dim y = 0
While y < outputData.Height
Dim line = New Byte(outputData.Stride - 1) {}
For x = 0 To input.Width - 1
Dim j = data(x, y) > 0
Try
If j Then
line(x / 8) = line(x / 8) Or masks(x Mod 8)
End If
Catch ex As Exception
End Try
Dim [error] = CSByte((data(x, y) - (If(j, 32, -32))))
If x < input.Width - 1 Then data(x + 1, y) += CSByte((7 * [error] / 16))
If y < input.Height - 1 Then
If x > 0 Then data(x - 1, y + 1) += CSByte((3 * [error] / 16))
data(x, y + 1) += CSByte((5 * [error] / 16))
If x < input.Width - 1 Then data(x + 1, y + 1) += CSByte((1 * [error] / 16))
End If
Next
Marshal.Copy(line, 0, scanLine, outputData.Stride)
y += 1
scanLine += outputData.Stride
End While
Finally
output.UnlockBits(outputData)
End Try
Return output
End Function
Public Shared Function GetGreyLevel(ByVal r As Byte, ByVal g As Byte, ByVal b As Byte) As Double
Return (r * 0.299 + g * 0.587 + b * 0.114) / 255
End Function
Private Sub btBrowesIn_Click(sender As Object, e As EventArgs) Handles btBrowesIn.Click
FolderBrowserDialog1.ShowDialog()
tbInPic.Text = FolderBrowserDialog1.SelectedPath
End Sub
Private Sub btBrowesOut_Click(sender As Object, e As EventArgs) Handles btBrowesOut.Click
FolderBrowserDialog2.ShowDialog()
tbInPic.Text = FolderBrowserDialog2.SelectedPath
End Sub
Private Sub btGo_Click(sender As Object, e As EventArgs) Handles btGo.Click
Dim Infiles As Array
Dim opf As New OpenFileDialog
opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"
If opf.ShowDialog = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(opf.FileName)
Dim MyBitmap As New Bitmap(PictureBox1.Image)
PictureBox2.Image = ConvertTo1Bit(MyBitmap)
End If
End Sub
End Class
The program will be used to do 100 to 300 images per batch.
You can process the images asynchronously. .Net provides several ways to do this: Async/Await, raw Tasks, ThreadPool, raw Threads, BackgroundWorker, probably more. Which is most appropriate here depends on the context of the application.
I'm creating a print preview function in a system that I'm developing which will preview the datagridview that I want to print. I used ooopsoft's codes as reference and it works fine except for a slight problem.
Problem:
In the you can see that the dgv row with serial number 1 is missing. It appears the header has overwritten the 1st row. I have tried a myriad of ways to solve it, but I still can't find the solution. I tried exiting the print preview dialog and opening it again, but this is the result I got. I think I'm missing a line of code, but I can't figure out what. Please help.
The original code is a nice start but has a couple of bugs and inefficiecies:
It uses the newpage flag to print the header or the first row when there is a new page. Obviously you really want it to do both
Printing the column headers is done once per page, so it doesnt need to be in the data print loop at all
It is not allowing for invisible columns or columns with other than default alignment, There could be other such settings you want to account for.
Because it is not actually printing the correct number of rows, once you fix that you'll find that it reprints the last row from the previous page as the first row of a new page.
There is an internal gutter or margin so that text does not print too close to gridlines - this just uses an offset of 1 or 2
It is also needlessly using single and RectangleF
It is also not prepared for the Document to be shown again or Printed. You will also want to reset mRow and newpage either in the button click or BeginPrint event.
I added a few comments as well as coloring the header row and demonstrating how to implement things like a RowPrePaint rule.
Private mRow As Integer = 0
Private newpage As Boolean = True
Private Sub PrintDocument1_PrintPage(sender As System.Object,
e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
' sets it to show '...' for long text
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Int32 = e.MarginBounds.Top
Dim rc As Rectangle
Dim x As Int32
Dim h As Int32 = 0
Dim row As DataGridViewRow
' print the header text for a new page
' use a grey bg just like the control
If newpage Then
row = dgvZZ.Rows(mRow)
x = e.MarginBounds.Left
For Each cell As DataGridViewCell In row.Cells
' since we are printing the control's view,
' skip invidible columns
If cell.Visible Then
rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
e.Graphics.FillRectangle(Brushes.LightGray, rc)
e.Graphics.DrawRectangle(Pens.Black, rc)
' reused in the data pront - should be a function
Select Case dgvZZ.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
Case DataGridViewContentAlignment.BottomRight,
DataGridViewContentAlignment.MiddleRight
fmt.Alignment = StringAlignment.Far
rc.Offset(-1, 0)
Case DataGridViewContentAlignment.BottomCenter,
DataGridViewContentAlignment.MiddleCenter
fmt.Alignment = StringAlignment.Center
Case Else
fmt.Alignment = StringAlignment.Near
rc.Offset(2, 0)
End Select
e.Graphics.DrawString(dgvZZ.Columns(cell.ColumnIndex).HeaderText,
dgvZZ.Font, Brushes.Black, rc, fmt)
x += rc.Width
h = Math.Max(h, rc.Height)
End If
Next
y += h
End If
newpage = False
' now print the data for each row
Dim thisNDX As Int32
For thisNDX = mRow To dgvZZ.RowCount - 1
' no need to try to print the new row
If dgvZZ.Rows(thisNDX).IsNewRow Then Exit For
row = dgvZZ.Rows(thisNDX)
x = e.MarginBounds.Left
h = 0
' reset X for data
x = e.MarginBounds.Left
' print the data
For Each cell As DataGridViewCell In row.Cells
If cell.Visible Then
rc = New Rectangle(x, y, cell.Size.Width, cell.Size.Height)
' SAMPLE CODE: How To
' up a RowPrePaint rule
'If Convert.ToDecimal(row.Cells(5).Value) < 9.99 Then
' Using br As New SolidBrush(Color.MistyRose)
' e.Graphics.FillRectangle(br, rc)
' End Using
'End If
e.Graphics.DrawRectangle(Pens.Black, rc)
Select Case dgvZZ.Columns(cell.ColumnIndex).DefaultCellStyle.Alignment
Case DataGridViewContentAlignment.BottomRight,
DataGridViewContentAlignment.MiddleRight
fmt.Alignment = StringAlignment.Far
rc.Offset(-1, 0)
Case DataGridViewContentAlignment.BottomCenter,
DataGridViewContentAlignment.MiddleCenter
fmt.Alignment = StringAlignment.Center
Case Else
fmt.Alignment = StringAlignment.Near
rc.Offset(2, 0)
End Select
e.Graphics.DrawString(cell.FormattedValue.ToString(),
dgvZZ.Font, Brushes.Black, rc, fmt)
x += rc.Width
h = Math.Max(h, rc.Height)
End If
Next
y += h
' next row to print
mRow = thisNDX + 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
' mRow -= 1 causes last row to rePrint on next page
newpage = True
Return
End If
Next
End Sub
Note that there is an Id column set to invisible in the DGV, the Color column is centered and Price is left justified - these are all settings picked up from the control. Also note that the text is moved away from the gridlines just a bit.
The last bullet point above, You will also want to reset mRow and newpage either in the button click or BeginPrint event. means this:
Private Sub PrintDocument1_BeginPrint(sender As Object,
e As PrintEventArgs) Handles PrintDocument1.BeginPrint
mRow = 0
newpage = True
PrintPreviewDialog1.PrintPreviewControl.StartPage = 0
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.0
End Sub
After you preview the mRow variable will indicate that all the rows have been printed. If the user clicks Print or goes back for another Preview, nothing will print. This code also resets the first page to show and the initial Zoom.
I searched before posting but couldn't find anything close to my issue.
What I need to figure out is how to come with the optimal width and height of picture boxes (with a 4:3 ratio), given the required number of boxes to be displayed, and the available space.
Now, it's not as simple as a just dividing the available space by the number of required boxes, because the available space is not a uniform shape, but rather two rectangles of which size may vary (see this picture, it's the a+b space).
If fact, I have tried starting from there with the following code :
Private Sub LayoutSnapshots()
Dim lTotalSpace As Single, lSnapsize As Single, sXSize As Single, sYSize As Single
Dim I As Integer, J As Integer, X As Integer = 0, Y As Integer = 0, oPic As PictureBox
' bSnaps is the number of picture boxes to be displayed
If stSetting.bSnaps = 0 Then Exit Sub
' oSnaps is a List(Of PictureBoxe) to groupp the actual picture boxes
If oSnaps.Count > 0 Then
For Each oCtrl As PictureBox In oSnaps
Me.Controls.Remove(oCtrl)
Next
End If
oSnaps.Clear()
' Calculating the a+b space shown on the picture
lTotalSpace = ((Me.ClientSize.Height - MenuStrip1.Height) * Me.ClientSize.Width) - ((picPreview.Width + iMargin) * (picPreview.Height + iMargin))
If lTotalSpace < 1 Then
MsgBox("Window is too small. Please adjust one of these settings : Window size, Snapshots count, Live free view size.", MsgBoxStyle.ApplicationModal Or MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
Exit Sub
End If
'calculating a single picture's size by dividing total space by the number of snaps
lSnapsize = Math.Truncate(lTotalSpace / stSetting.bSnaps)
'Calculating Height and Width, with 4:3 ratio
sXSize = Math.Truncate(Math.Sqrt((4 * lSnapsize) / 3))
sYSize = Math.Truncate(Math.Sqrt((3 * lSnapsize) / 4))
For I = 1 To stSetting.bSnaps
If oPic IsNot Nothing Then oPic = Nothing
oPic = New PictureBox
oPic.BackColor = Color.White
oPic.BorderStyle = BorderStyle.FixedSingle
oPic.Size = New Size(sXSize - 1, sYSize - 1)
oPic.Location = New Point(X * sXSize, (Y * sYSize) + MenuStrip1.Height)
oSnaps.Add(oPic)
' Layed them successively on screen, need to optimize this
If ((X + 2) * sXSize) > (Me.ClientSize.Width) Then
X = 0
Y += 1
Else
X += 1
End If
Next
For Each oCtrl As PictureBox In oSnaps
Me.Controls.Add(oCtrl)
Next
End Sub
But obviously with all the possibilities of windows resizing, I couldn't think of any practical way to optimize it.
I am pretty sure this has to do with "operation research", as I recall we did optimization problems like this back then when I was a student, but I'm not sure how to actually model this or even if it is solvable by linear programming.
I have figured this out. The solution is kind of a "brute force" technique, it doesn't always return the optimum BUT the error is merely a few pixels. I used the code below, it works but it might need further optimization in terms of spacing. I couldn't comment on everything since I have a time pressure right now, but still wanted to share the answer, so just take some time to analyze it :
Private Sub LayoutSnapshots()
Dim sA As Single, sB As Single, sTotal As Single, sSnap As Single, sWidth As Single, sHeight As Single
Dim iCount As Integer = stSetting.bSnaps, iFit As Integer, iX As Integer, iY As Integer, iYg As Integer, I As Integer
Dim rA As Rectangle, rB As Rectangle, oPic As PictureBox, lpLoc As New List(Of Point), pLoc As New Point
Static bWarn As Boolean
Dim gPic As Graphics
' bSnaps is the number of picture boxes to be displayed
If stSetting.bSnaps = 0 Then Exit Sub
' If controls already on form, remove them and start form scratch
If oSnaps.Count > 0 Then
For Each oCtrl As PictureBox In oSnaps
Me.Controls.Remove(oCtrl)
Next
End If
' oSnaps is a List(Of PictureBox) grooping the picture boxes. Clear it for now
oSnaps.Clear()
'sA, sB are the sizes of spaces A and B respectively
sA = (Me.ClientSize.Width * (Me.ClientSize.Height - (MenuStrip1.Height + picPreview.Height + iMargin)))
sB = ((Me.ClientSize.Width - (picPreview.Width + iMargin)) * (picPreview.Height + iMargin))
' Total free space
sTotal = sA + sB
' This condition is important. It ensures there is at least one solution
' before entering the loops bellow. Otherwise we might get stuck in an infinite loop
If (sTotal < (stSetting.bSnaps * stSetting.bSnaps)) Then
' bWarn is a static boolean. Since this Sub is called from Form_Resize event, we
' want to warn the user only once when there is no space.
' Otherwise it becomes annoying.
If bWarn Then MsgBox("Window is too small. Please adjust one of these settings : Window size, Snapshots count, Live free view size.", MsgBoxStyle.ApplicationModal Or MsgBoxStyle.Exclamation Or MsgBoxStyle.OkOnly)
bWarn = False
Exit Sub
End If
bWarn = True
Me.UseWaitCursor = True
Do
'rA, rB are the bounding rectangles of spaces A and B respectively
rA = New Rectangle(0, MenuStrip1.Height, Me.ClientSize.Width, Me.ClientSize.Height - (MenuStrip1.Height + picPreview.Height + iMargin))
rB = New Rectangle(0, picPreview.Top, Me.ClientSize.Width - (picPreview.Width + iMargin), picPreview.Height + iMargin)
' A single box's size
sSnap = Math.Truncate(sTotal / iCount)
' Width and Height with 4:3 aspect ratio.
sWidth = Math.Truncate(Math.Sqrt((4 * sSnap) / 3))
sHeight = Math.Truncate(Math.Sqrt((3 * sSnap) / 4))
' iFit keeps track of how many boxes we could fit in total
iFit = 0
iYg = 0
lpLoc.Clear()
' It would be a bit too long to explain the next block of code and I have a deadline to meet
' I'll comenting on that later
iX = 0
iY = 0
Do While (rA.Height >= ((sHeight * (iY + 1)) + 1))
If (((iX + 1) * sWidth) + 1) <= rA.Width Then
iFit += 1
lpLoc.Add(New Point(rA.X + ((iX * sWidth) + 1), rA.Y + ((iYg * sHeight) + 1)))
iX += 1
Else
iX = 0
iY += 1
iYg += 1
End If
Loop
'Add unused space from A to B.
rB.Height = rB.Height + (rA.Height - ((iYg * sHeight) + 1))
iX = 0
iY = 0
Do While (rB.Height >= ((sHeight * (iY + 1)) + 1))
If (((iX + 1) * sWidth) + 1) <= rB.Width Then
iFit += 1
lpLoc.Add(New Point(rB.X + ((iX * sWidth) + 1), rA.Y + ((iYg * sHeight) + 1)))
iX += 1
Else
iX = 0
iY += 1
iYg += 1
End If
Loop
Application.DoEvents()
iCount += 1
Loop While iFit < stSetting.bSnaps
' Add controls to form. Lay them one next to each other.
iX = 0
iY = 0
For I = 1 To stSetting.bSnaps
If oPic IsNot Nothing Then oPic = Nothing
oPic = New PictureBox
oPic.BackColor = Color.Cyan
oPic.BorderStyle = BorderStyle.FixedSingle
oPic.Size = New Size(sWidth - 1, sHeight - 1)
oPic.Location = lpLoc(I - 1)
' Just for debugging, displays index of each box inside it.
oPic.Image = New Bitmap(oPic.Width, oPic.Height)
gPic = Graphics.FromImage(oPic.Image)
gPic.DrawString(I, New Font("Arial", 10, FontStyle.Regular), Brushes.Red, New Point(0, 0))
oSnaps.Add(oPic)
Me.Controls.Add(oSnaps.Last)
Next
'Catch Ex As Exception
'Finally
Me.UseWaitCursor = False
'End Try
End Sub
P.S : Anyone please feel free to add more explanation to the code if you want.
i me using e.HasMorePages with code
For x As Integer = RowNo To dgv.Rows.Count - 1
Dim mypen As New Pen(Color.Black, 6)
e.Graphics.DrawString(dgv.Rows(x - 1).Cells(0).Value.ToString(), f, Brushes.Black, 645, yElementy)
If RowNo Mod 6 = 0 Then
RowNo += 1
e.HasMorePages = True
Exit For
End If
RowNo += 1
Next
how to using e.HasMorePages With code
For Each myRow In dtn.Rows
ListView1.Items.Add(n + ListView1.Items.Count + 1)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(13).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(5).ToString())
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myRow.Item(14).ToString())
'i want to add here
Next
Typical layout: count variable(class level) for remembering what position we are at in the items printing. I like a List(Of String) for looping and printing. Inside the printPage event you need to have a variable for what row(y axis) your printing the records to and increment it with each iteration. Since it's graphics based you can also use a Rectangle structure and print contents to it using a StringFormat object for text wrapping and layout.
Print Document
Example off the top of my head - not tested.
Public Class Form1 ' your form name here
Private count As Integer
Private row As Integer
Private Sub print_Page(...) Handles ...
row = 100 'starting point from the top
Using p As New Pen(Brushes.Bisque) 'self disposing graphics object
'we use the variable here to know where we are if we have to go to next page
Dim rowCount = dg.Rows.Count - 1
For i As Integer = count To rowCount
e.Graphics.DrawString({value},p, Font, x, y)
row += 16 'basically the font height and some space in-between
If row = e.MarginBounds.Bottom - 20 Then
e.HasMorePages = True
If i <> rowCount Then 'are we on the last row?
count = i ' remember where we left off
Exit Sub ' cause this event will fire again and we need to start over
End If
End If
Next
End Using
End Sub
'...
End Class
Public Class frmTestHasMorePages
Dim Font12 As Font = New Drawing.Font("Arial", 12, FontStyle.Regular)
Dim Font8 As Font = New Drawing.Font("Arial", 8, FontStyle.Regular)
Dim sBrush As Drawing.Brush
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static count As Integer
Static topMargin As Integer = 50
Static line As Integer
Static s, z As Integer
Dim linesPerPage = 65
Dim lineCount = 350
Static totalpages As Integer = IIf(lineCount Mod linesPerPage = 0, (lineCount / linesPerPage), (lineCount / linesPerPage) + 1)
sBrush = Brushes.Black
Dim pageBottom = e.PageBounds.Bottom
For i As Integer = count To lineCount
If z > lineCount Then
sBrush = Brushes.Blue
e.Graphics.DrawString("Page " & s + 1 & "/" & totalpages, Font8, sBrush, 750, pageBottom - 20)
sBrush = Brushes.Red
e.Graphics.DrawString("End of document ", Font8, sBrush, 50, pageBottom - 20)
e.HasMorePages = False
Exit Sub
End If
e.Graphics.DrawString("Testing hasmorepages with different options " & z, Font12, sBrush, 45, (i * 16) + topMargin)
line += 15
z += 1
If i = linesPerPage AndAlso s <= totalpages Then
e.HasMorePages = True
sBrush = Brushes.Blue
s += 1
e.Graphics.DrawString("Page " & s & "/" & totalpages, Font8, sBrush, 750, pageBottom - 20)
e.Graphics.DrawString("Continued...", Font8, sBrush, 50, pageBottom - 20)
i = 0
If i <> lineCount Then REM if it is the last line to print
Exit Sub
End If
End If
Next
End Sub
End Class
I am computing the ROI with a moving rectangle and extracting the ROI to compute the standard deviation, mean, area and Pixel value coordinates X and Y in a seperate form2 by clicking the mouse. At this juncture I am trying to pass a function from the main Form that loads the Image and displays the rectangle to another Form that has the displayed properties of the mean and standard deviation etc. However, I'm receiving errors in runtime in the function that contains the standard deviation. The error displayed is
Index was outside the bounds of the array.
It is displayed at the end of this portion of the code in the function StD, i.e at the end of the mean part'
SD(count) = Double.Parse(pixelcolor.R) + Double.Parse(pixelcolor.G) + Double.Parse(pixelcolor.B) - mean
what is this actually saying and how can I fix this situation. Any tips and ideas, thanks.
My code is at the bottom
enterPublic Function StD(ByVal image As Bitmap, ByVal mean As Double, ByVal meancount As Integer) As Double
Dim SD(SquareHeight * SquareWidth) As Double
Dim count As Integer = 0
For i = 0 To SquareWidth
For j = 0 To SquareHeight
Dim pixelcolor As Color = image.GetPixel(i, j)
SD(count) = Double.Parse(pixelcolor.R) + Double.Parse(pixelcolor.G) + Double.Parse(pixelcolor.B) - mean
count += 1
Next
Next
Dim SDsum As Double = 0
For i = 0 To count
SDsum = SDsum + SD(i)
Next
SDsum = SDsum / (SquareHeight * SquareWidth)
SDsum = ((SDsum) ^ (1 / 2))
Return SDsum
End Function code here
I would like to pass this using the code below
enterPrivate Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Dim mean As Double = 0
Dim meancount As Integer = 0
Dim bmap As New Bitmap(400, 400)
bmap = PictureBox1.Image
Dim colorpixel As Color = bmap.GetPixel(e.X, e.Y)
' Dim pixels As Double = colorpixel.R + colorpixel.G + colorpixel.B
If e.Button = Windows.Forms.MouseButtons.Left AndAlso Rect.Contains(e.Location) Then
If (PictureBox1.Image Is Nothing) Or (PictureBox1.Height - (e.Y + SquareHeight) < 0) Or (PictureBox1.Width - (e.X + SquareWidth) < 0) Then
Else
Dim ROI As New Bitmap(400, 400)
Dim x As Integer = 0
Dim countx As Integer = 0
Dim county As Integer = 0
For i = e.X To (e.X + SquareWidth)
For j = (e.Y + x) To (e.Y + SquareHeight)
Dim pixelcolor As Color = bmap.GetPixel(i, j)
ROI.SetPixel(countx, county, pixelcolor)
mean = mean + pixelcolor.R + pixelcolor.G + pixelcolor.B
county += 1
meancount += 1
Next
county = 0
countx += 1
x = x + 1
Next
mean = mean / (meancount * 3)
Dim SD = mean - 75
Dim area As Integer = (SquareHeight * SquareWidth)
Dim anotherForm As Form2
anotherForm = New Form2(mean, StD(bmap, mean, meancount), area, 34)
anotherForm.Show()
End If
End If
' Catch ex As Exception
' MessageBox.Show(ex.Message())
' End Try
End Sub code here
To be displayed with this code
enter Public Sub New(ByVal mean As Double, ByVal StD As Double, ByVal Area As Integer, ByVal pixel As Double)
MyBase.New()
InitializeComponent()
TextBox1.Text = mean.ToString()
TextBox2.Text = StD.ToString()
TextBox3.Text = Area.ToString()
TextBox4.Text = pixel.ToString()
End Sub code here
The problem probably is because of these lines:
For i = 0 To SquareWidth
For j = 0 To SquareHeight
Try using this instead:
For i = 0 To SquareWidth - 1
For j = 0 To SquareHeight - 1