I have a picture box array set up
Dim i = 7
Dim s As PictureBox() = New PictureBox(0) {}
s(0) = New PictureBox()
s(0).Image = Image.FromFile("C:\Pic Folder\pic.png")
s(0).Location = New Point(10, 10)
s(0).Size = New Size(50, 50)
however, no image shows up at all. before, when i had declared a new picturebox and put the specifications as the new picturebox (say, foo.Image as opposed to s(0)) it worked fine
Related
My application uses a panel to resemble a template that the user fills in and then prints.
However, they can end up having over 300 pages and my routine that makes the PDF slows down, takes up too much memory and then ultimately crashes with the message of either "OutofMemoryException" or "Invalid Parameter".
The method I use is; loading the revelant information onto the page and then turning it into a bitmap image to place on the PDFPage.
As discovered this really hasn't worked, any suggestions?
Btw I use PDFSharp to make this.
Routine that handles this;
For i = 0 To Count - 1
If i = 0 Then ConditionReportConfig.SwitchPage5(Pages(i), False) Else ConditionReportConfig.SwitchPage5(Pages(i), True)
If WithPics = True And ConditionReportConfig.PG5.PbxAddress(2) <> "" And ConditionReportConfig.PG5.txt_Occupier5.Enabled = True Then
frm_MsgAttachment.pbx_Picture.Image = Image.FromFile(ConditionReportConfig.PG5.PbxAddress(2))
Dim PicPage As PdfPage = PDFDoc.AddPage
PicPage.Orientation = PageOrientation.Landscape
Dim xgr As XGraphics = XGraphics.FromPdfPage(PicPage)
Dim img As XImage
Dim xBitmap As Bitmap
PicPage.Orientation = PageOrientation.Landscape
xBitmap = New Bitmap(2480, 3508)
frm_MsgAttachment.pbx_Picture.DrawToBitmap(xBitmap, New Rectangle(0, 0, xBitmap.Width, xBitmap.Height))
img = xBitmap
xgr.DrawImage(img, 6, 5, 2300, 3508)
xgr.Dispose()
xgr = Nothing
xBitmap.Dispose()
xBitmap = Nothing
GC.Collect()
End If
Dim CDR5 As PdfPage = PDFDoc.AddPage
CDR5.Orientation = PageOrientation.Landscape
GFX = XGraphics.FromPdfPage(CDR5)
Bitmap = New Bitmap(ConditionReportConfig.PG5.Panel1.Width, ConditionReportConfig.PG5.Panel1.Height)
ConditionReportConfig.PG5.Panel1.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Bitmap.Height))
BXImage = Bitmap
GFX.ScaleTransform(0.82)
GFX.DrawImage(BXImage, 0, 25)
GFX.Dispose()
GFX = Nothing
Dim valu = frm_Main.pgb_Load.Value
frm_Main.SetPrgBarValue(valu + tem)
Next
So basically what I wish to accomplish is that I set total of Pictureboxes to add and each picturebox has its own according label and if we reach 6 images we allow the scrollbar to go down.
Example:
http://i.imgur.com/5xWu5Y1.png
As you can see pictures are added accordingly with label.
How can I do this in code?
Currently I got something like this:
' Total PictureBoxes/Labels to Generate
Dim wTotalPictures = 1
' Original PictureBox Starting Point
Dim wOriginalLocation = New System.Drawing.Point(40, 54)
' Assigned Label Title
Dim wImageTitle As String = "PictureTitle"
Dim wNewImage As New PictureBox
Dim wImageSize = New Size(122, 173)
With wNewImage
.SizeMode = PictureBoxSizeMode.StretchImage
.Image = Image.FromFile("C:\Users\Jason\Pictures\newImage.jpg")
.Location = wOriginalLocation
.Size = wImageSize
End With
So any help would be greatly appreciated!
EDIT:
I've now managed to change it into this, this works way better but still not 100% automatic like I want it.
' Total PictureBoxes/Labels to Generate
Dim i As Integer = 0
Dim wTotalPictures = 5
' Original PictureBox Starting Point
Dim wOriginalLocation = New System.Drawing.Point(40, 54)
' Assigned Label Title
Dim wImageTitle As String = "PictureTitle"
Dim wImageSize = New Size(122, 173)
Dim wNewLocation As Integer = 0
Do Until i = wTotalPictures
' Setup
Dim wNewImage(i) As PictureBox
wNewImage(i) = New PictureBox
' Execute
wNewImage(i).SizeMode = PictureBoxSizeMode.StretchImage
wNewImage(i).Image = Image.FromFile("C:\Users\Jason\Pictures\newImage.jpg")
wNewImage(i).Size = wImageSize
If wNewLocation < 480 Then
wNewImage(i).Location = New System.Drawing.Point(40 + wNewLocation, 54)
ElseIf wNewLocation = 480 Then
wNewImage(i).Location = New System.Drawing.Point(40, 258)
wNewLocation = 0
End If
' Add to Form
MyTab.Controls.Add(wNewImage(i))
wNewImage(i).BringToFront()
wNewLocation = wNewLocation + 160
i += 1
Loop
Please help, thanks!
I found an example online of how to create a business card listview item using the ObjectListView. It demonstrates using a custom renderer to manually draw each Tile within the control.
The sample involves creating buffered graphics and manually drawing each item. I have this working, however, I am finding that all text I draw on the tile is looking pixelated (no matter what settings I use), especially if I draw small text. For example, using a default system font on a normal form looks fine, but in my renderer it looks jagered.
The code looks like this:
Imports BrightIdeasSoftware
Imports System.Drawing.Drawing2D
Imports System.IO
Public Class StoreListRenderer
Inherits AbstractRenderer
Public Overrides Function RenderItem(e As DrawListViewItemEventArgs, g As Graphics, itemBounds As Rectangle, rowObject As Object) As Boolean
' If we're in any other view than Tile, return false to say that we haven't done
' the rendereing and the default process should do it's stuff
Dim olv As ObjectListView = TryCast(e.Item.ListView, ObjectListView)
If olv Is Nothing OrElse olv.View <> View.Tile Then
Return False
End If
' Use buffered graphics to kill flickers
Dim buffered As BufferedGraphics = BufferedGraphicsManager.Current.Allocate(g, itemBounds)
g = buffered.Graphics
g.Clear(olv.BackColor)
g.SmoothingMode = ObjectListView.SmoothingMode
g.TextRenderingHint = ObjectListView.TextRenderingHint
If e.Item.Selected Then
Me.BorderPen = Pens.White
Me.HeaderBackBrush = New SolidBrush(olv.HighlightBackgroundColorOrDefault)
Else
Me.BorderPen = New Pen(Color.FromArgb(&H33, &H33, &H33))
Me.HeaderBackBrush = New SolidBrush(Color.FromArgb(&H33, &H33, &H33))
End If
DrawStoreCard(g, itemBounds, rowObject, olv, DirectCast(e.Item, OLVListItem))
' Finally render the buffered graphics
buffered.Render()
buffered.Dispose()
' Return true to say that we've handled the drawing
Return True
End Function
Friend BorderPen As New Pen(Color.FromArgb(&H33, &H33, &H33))
Friend TextBrush As Brush = New SolidBrush(Color.FromArgb(&H22, &H22, &H22))
Friend HeaderTextBrush As Brush = Brushes.AliceBlue
Friend HeaderBackBrush As Brush = New SolidBrush(Color.FromArgb(&H33, &H33, &H33))
Friend BackBrush As Brush = Brushes.LemonChiffon
Friend BackgroundBrush As Brush = New SolidBrush(Color.FromArgb(38, 38, 38))
Public Sub DrawStoreCard(g As Graphics, itemBounds As Rectangle, rowObject As Object, olv As ObjectListView, item As OLVListItem)
Try
Dim _store As StoreObject = TryCast(rowObject, StoreObject)
' Allow a border around the card
itemBounds.Inflate(-5, -5)
g.FillRectangle(BackgroundBrush, itemBounds)
Dim ColouredPanelRect As Rectangle = New Rectangle(itemBounds.Left + 7, itemBounds.Top + 7, 70, 70)
g.FillRectangle(Brushes.IndianRed, ColouredPanelRect)
Dim fmt As New StringFormat()
fmt.Alignment = StringAlignment.Center
fmt.LineAlignment = StringAlignment.Center
For i As Integer = 0 To olv.Columns.Count - 1
Dim column As OLVColumn = olv.GetColumn(i)
If column.IsTileViewColumn Then
'Draw Store Number
Using font As New Font(fontMgr("Mentone Lig"), 36, FontStyle.Regular, GraphicsUnit.Pixel)
g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.DrawString(_store.StoreID, font, BackgroundBrush, ColouredPanelRect, fmt)
End Using
'Draw Store Name
Using string_format As New StringFormat()
string_format.Alignment = StringAlignment.Center
string_format.LineAlignment = StringAlignment.Near
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAliasGridFit
g.DrawString(_store.StoreName.ToUpper, New Font("Segoe UI", 9, FontStyle.Regular), Brushes.White, itemBounds, string_format)
End Using
...
I have tried playing around AntiAlias and ClearType but both have the same affect.
UPDATE
Here are my results below with using the same font at the same size:
The text at the top represents a normal label placed on a form. The text at the bottom is text drawn on to a listview item (in Tile mode) using the code above. You can easily identify the difference and see that it is not as smooth. Also the larger I make the text the more unsmooth it gets. I have tried playing around with all the settings and currently have it set to:
g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.PixelOffsetMode = PixelOffsetMode.HighQuality
Thanks
Okay, I ended up working this out by going through the example again.
The problem was that I was dropping the scaling element from my code which seemed to be causing the pixilation of the text.
By adding the following code back in, it has corrected the smoothness of the text:
Dim size As SizeF = g.MeasureString(txt, font, CInt(itemBounds.Width), fmt)
g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
So my overall code looks like this:
Using font As New Font("Segoe UI", 9, FontStyle.Regular)
' Measure the height of the title
txt = column.GetStringValue(rowObject)
Dim size As SizeF = g.MeasureString(txt, font, CInt(itemBounds.Width), fmt)
g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.PixelOffsetMode = PixelOffsetMode.HighQuality
g.DrawString(txt, font, Brushes.LightGray, itemBounds, fmt)
End Using
I have my pixBox1 which is fix, and unable to change the image
and pixBox2 is not fix which can change there color and rotate in here
i use OpenFileDialog function to put image inside those pixbox
so now how can i combine those two pixbox into my pixbox3?
i try this but it doesn't look like it's going to work:
Dim image As New Bitmap(pixBox1.Image)
Dim image2 As New Bitmap(pixBox2.Image)
Dim Image3 As New Bitmap(300, 300)
Dim g As Graphics = Graphics.FromImage(Image3)
g.DrawImage(image1, New Point(300, 300))
g.DrawImage(image2, New Point(300, 300))
g.Dispose()
g = Nothing
pixBox3.Image = Image3
This is kinda ugly and slow, but it basically sets all the pixels in image2 to be 50% transparent then draws it over the top of Image.
Dim image As New Bitmap(pixBox1.Image)
Dim image2 As New Bitmap(pixBox2.Image)
Dim Image3 As New Bitmap(300, 300)
Using g As Graphics = Graphics.FromImage(Image3)
'make 2nd bmp translucent
For Integer Xcount = 0 To image2.Width - 1
For Integer Ycount = 0 To image2.Height - 1
Dim c as Color = image2.GetPixel(Xcount, Ycount)
c = Color.FromARGB(125, c.R, c.G, c.B) '50% alpha
image2.SetPixel(Xcount, Ycount, c)
Next
Next
g.DrawImage(image1, New Point(0, 0))
g.DrawImage(image2, New Point(0, 0))
End Using
pixBox3.Image = Image3
As a side note, the Using block makes sure g is disposed no matter what happens.
I am trying to figure out how to go about creating dynamic checkboxes on my form when I do not know exactly how many boxes I will need.
The problem is that I do not know how to DIM more than one object. This is my code for creating one checkbox
Dim checkBox As New CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(10, 10)
checkBox.Text = "testing"
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
It works just fine but i am unable to add more than one checkBox without having to do this:
Dim checkBox As New CheckBox()
Dim checkBox2 As New CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(10, 10)
checkBox.Text = "testing"
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
Form1.Controls.Add(checkBox2)
checkBox2.Location = New Point(40, 10)
checkBox2.Text = "testing2"
checkBox2.Checked = True
checkBox2.Size = New Size(100, 20)
etc...
Is there a way to dim more than 1 checkbox instead of having to write multiple dim statements for each checkBoxe?
Sorry maybe i should say this..
I'm looking to do something like this:
dim checkBox() as CheckBox
do until i = 50
Form1.Controls.Add(checkBox(i))
checkBox(i).Location = New Point(10, 10)
checkBox(i).Text = "testing " & i
checkBox(i).Checked = True
checkBox(i).Size = New Size(100, 20)
i += 1
loop
It seems like the only items that are different and not calculated between the CheckBox instances is the text. If so then you could just use the following code to add a set of CheckBox instances based off of a list of String's.
Dim data as String() = New String() { "testing", "testing2" }
Dim offset = 10
For Each cur in data
Dim checkBox = new CheckBox()
Form1.Controls.Add(checkBox)
checkBox.Location = New Point(offset, 10)
checkBox.Text = cur
checkBox.Checked = True
checkBox.Size = New Size(100, 20)
offset = offset + 30
Next
Put it in a loop, including the new statement but varing the position.
You could also clone the object, maybe with performance penalties ... Sorry but don't know Vb.net, I will give you the c# code hoping it will be similar. I think this it is not the best solution for your case (a loop will do the trick), but maybe it will be for someone with a similar but more generic problem.
CheckBox CB2 = (CheckBox)CloneObject(CheckBox1);
//change the location here...
Form1.Controls.Add(checkBoxCB2 )
private object CloneObject(object o)
{
Type t = o.GetType();
PropertyInfo[] properties = t.GetProperties();
Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, o, null);
foreach(PropertyInfo pi in properties)
{
if(pi.CanWrite)
{
pi.SetValue(p, pi.GetValue(o, null), null);
}
}
return p;
}