Spanning a single image across multiple pictureboxes (Visual Basic) - vba

I don't have any code because I really don't know where to begin on something like this. I am using Visual Basic. I have two picture boxes which are proportioned apart as the screens are on the 3DS. What I want is to be able to open an image file and have that image span across both pictureboxes as one. Just like on the Nintendo 3DS, where the screens are connected. The reason I am doing this is so I can crop the image into two perfect "screens" which will tile vertically together perfectly. I am making a 3DS Theme Cropper. I am sorry that I don't have any code already. I just cannot think of the first thing to begin accomplishing this. I am also sorry if my question isnt good enough. I am not sure what this website is looking for. Grammar? The right wording? I dont know.
here is an rough diagram of what I mean: https://www.dropbox.com/home?preview=samplepix.png

play with something like this, but not really a question with no code.
Sub threeds()
Dim p As Picture
Set p = ActiveSheet.Pictures(1)
p.ShapeRange.PictureFormat.Crop.PictureOffsetX = p.Width / 2
Set p = ActiveSheet.Pictures(2)
p.ShapeRange.PictureFormat.Crop.PictureOffsetX = -(p.Width / 2)
End Sub

I found some code that seems to work.
Private Function CropBitmap(ByRef bmp As Bitmap, ByVal cropX As Integer, ByVal cropY As Integer, ByVal cropWidth As Integer, ByVal cropHeight As Integer) As Bitmap
Dim rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
Dim cropped As Bitmap = bmp.Clone(rect, bmp.PixelFormat)
Return cropped
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim FN = "full path of the image file"
Dim bmp As Bitmap bmp = Bitmap.FromFile(FN)
PictureBox1.Image = CropBitmap(bmp, 4, 4, 13, 16)
End Sub

Related

Dynamically changing image in picture box not working

I've been trying to figure this out off-and-on for weeks.
In my VB 2010 Forms application, I have a number of picture boxes which are populated with images from other picture boxes using the drag-and-drop method. That's no problem, it works fine. The picture boxes are all in a groupbox container.
The problem is trying to swap images between two picture boxes on a drag-and drop operation. In other words pBox1 has image.x and pBox2 has image.y. Drag the image from pBox2 to pBox1, and drop it; pBox1 will then have image.y from pBox2 and pBox2 will have image.x from pBox1.
With this example, here's the code I have so far:
Private Sub pBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox1.MouseDown
strImageSource = "pBox2" 'strImageSource is a global string variable
[other stuff]
end sub
^ This saves the name of the source picture box to a global string.
Private Sub pBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pBox1.DragDrop
For Each Control as PictureBox in GroupBox1.Controls.OfType(of PictureBox)()
if Control.Name = strImageSource then
Control.Image = pBox1.Image
end if
next
dim imgTarget as Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
pBox1.image = imgTarget
End Sub
^ This searches for the picture box as named by the strImageSource ("pBox2") and copies the contents of pBox1 into it, and then drops the image that was in pBox2 into pBox1.
I hope this makes sense.
This correctly places the image from pBox2 into pBox1, but it does not switch the image from pBox1 into pBox2. pBox2 is just blank. However, debugging shows that the image in pBox2 is not nothing; it does contain a bitmap. It's just not visible.
Now, just as a test, I added a line to the For Each section that would change the background color of the picture box:
For Each Control as PictureBox in GroupBox1.Controls.OfType(of PictureBox)()
if Control.Name = strImageSource then
Control.Image = pBox1.Image
Control.BackColor = color.red
end if
next
And the back color does change. This tells me that the For Each section is working -- it's finding the control and changing the back color. It's just not showing the image.
Is there something I am overlooking?
Thanks!
Instead of using strImageSource, use a global variable defined as a Picturebox
Private tmpPictureBox As PictureBox
Then store that picturebox reference so you can set its image on DragDrop
Private Sub pBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox1.MouseDown
'store the picturebox reference
tmpPictureBox = sender
[other stuff]
end sub
Private Sub pBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pBox1.DragDrop
'set the first image to the second
tmpPictureBox.Image = sender.image
'set the second image to the first
pBox1.image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap)
End Sub
You should call Control.Refresh() on the PictureBox controls in order to update the images.
OK, this was stupid.
I was doing everything right with one really boneheaded exception. In another part of the code, inexplicably, I was clearing the picture boxes of content after the image was replaced. It may have been a remnant of something I was trying to do unrelated to this problem, and I just never corrected it.
My apologies for that, and thanks for all the responses.

Visual Basic drawpolygon method using a do-while loop?

I am looking for help with the drawpolygon method. I haven't had any luck getting anything to work. Looking to do 5 triangles, same size, next to each other. Problem is I have to use a do-while loop. Thanks for taking the time to help me!!
Just define the points in an array an then write them with a pen:
Dim blackPen As New Pen(Color.Black, 3)
Dim point1 As New Point(50, 50)
Dim point2 As New Point(100, 25)
Dim curvePoints As Point() = {point1, point2}
Me.CreateGraphics.DrawPolygon(blackPen, curvePoints)
Take a look at the MSDN Documentation about it.
It doesn't matter if you do anything in a loop, depends how. If this didn't solve your problem post your code in order to help you more.
Example with a loop:
Do While i < 3
point1 As New Point(50 + i * 10, 50)
point2 As New Point(100 + i * 7, 25)
curvePoints = {point1, point2}
Me.CreateGraphics.DrawPolygon(blackPen, curvePoints)
i += 1
Loop
I haven't actually tested this construction but it's all snipped out of a working project; I suspect it'll work and some of this GDI+ stuff is really grungy to pick up the first time.
Public Class Form1
Private subject As Image
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If Not subject Is Nothing Then
Dim g As Graphics = e.Graphics
g.DrawImage(subject, New Point(1, 1))
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tempBM As New Bitmap(subject)
tempBM.SetResolution(subject.HorizontalResolution, subject.VerticalResolution)
Using g As Graphics = Graphics.FromImage(tempBM)
g.DrawPolygon(OutlinePen, Polygon.GetPoints)
End Using
subject = tempBM
Invalidate()
End Sub
End Class
Oh, Polygon is a class in my code that you won't have. But just replace Polygon.GetPoints with whatever array of points you want to use.

Vb.net PRTSC (Screen capture) - Error

Hi again my question this time its related with this piece of code, im using the visual studio beta 2012, i cant seem to find the issue, if you guys could help me out ill appreciate it
Public Class Form1
Private Sub fullScreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fullScreen.Click
SendKeys.SendWait("^{PRTSC}")
Dim clip As IDataObject = Clipboard.GetDataObject()
If clip.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
Dim screenCapture As Bitmap = CType(clip.GetData(GetType(System.Drawing.Bitmap)), Bitmap)
screenCapture.Save("C:\fullScreenCapture.bmp")
End If
Clipboard.Clear()
End Sub
End Class
Error :
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: Error genérico en GDI+.
If there is a handler for this exception, the program may be safely continued.
You can take a screen shot more easily by using the following (send keys is always hit and miss in my experience)
Private Function TakeScreenShot() As Bitmap
Dim scrn As Screen = Screen.FromControl(Me)
Dim screenSize As Size = New Size(scrn.Bounds.Width, scrn.Bounds.Height)
Dim screenGrab As New Bitmap(screenSize.Width, screenSize.Height)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(scrn.Bounds.X, scrn.Bounds.Y), New Point(0, 0), screenSize)
Return screenGrab
End Function
Are you trying to capture the screen? Why not use VS' classes to capture the screen?
http://forum.codecall.net/topic/51761-creating-a-screen-shot-tool-vbnet

Trying to draw on a Picture Box, but nothing appears

I'm converting a VB6 application to VB.Net that draws on picture boxes. Naturally I read the fine manual and turn up this example here. I therefore produced a little project with a form containing only a picture box and tried the following:-
Private Sub Picture1_paint(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles PictureBox1.Paint
Dim mygraphics As Graphics
mygraphics = PictureBox1.CreateGraphics
Dim pen As New Drawing.Pen(System.Drawing.Color.Red, 1)
mygraphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose
End Sub
just like it says. But on running the application, the box turns up blank. Searching for help turned up a suggestion here that I should use a Frame instead, but the result was the same. I have checked that I'm not drawing in the background colour, and that the function is actually invoked.
What have I overlooked?
Paint handler has invalid type for EventArgs. It should be System.Windows.Forms.PaintEventArgs
Use e.Graphics property to obtain graphics instance.
mygraphics = e.Graphics
Reference Link MSDN - Control.Paint Event
I think e is of PainEventArgs type, with already contains a graphics object in e.Graphics. Use that instead.
Public Class Form1
Private Sub PictureBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim pen As New Pen(Color.Red, 1)
e.Graphics.DrawEllipse(pen, 0, 0, 100, 100)
pen.Dispose()
End Sub
End Class

Dynamically resize font to fill up RichTextbox

How do i dynamically resize the text in a RichTextbox so that it fills up the entire rich textbox?
any help is much appreciated. thank you.
This MSDN article almost answers your question. http://msdn.microsoft.com/en-us/library/bb986765.aspx.
You may download the attached sample there.
I think you might have to get creative with the 'Font' constructor. For example, on a click event then construct a new Font using some relationship with your application (or textbox size) with the desired font size.
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
Dim yourfontsize As Integer
'machinery to create desired font size
If blah then
yourfontsize = X()
Else
yourfontsize = Y()
End If
yourtextbox.SelectionFont = New Font("Tahoma", yourfontsize, FontStyle.Bold)
End Sub
Where X() and Y() are functions to return your target font sizes based on whatever else may
be going on within your application.
Reference: http://msdn.microsoft.com/en-us/library/yh8963yx.aspx
Hope that helps!
-sf