Resize image when pasted to RichTextBox - or another solution? - vb.net

I have a Windows form with a TabControl. In Tab 1 I have a RichTextBox (rtb1) and a Picturebox (pbx). In Tab 2 I have another RichTextBox (rtb2).
When I click a button, text from rtb1 and the current picture in pbx is put into rtb2. Problem is that the picturesize won't always fit. My current code is this:
Dim text = rtb1.Text
rtb2.AppendText(text)
Clipboard.SetImage(pbx.Image)
rtb2.Paste()
Is it possible to have the code resize the image as it's pasted?
It is possible to get the image from a windows path, I just didn't know how to resize this in a RichTextBox either. If this makes it easier?
Edit: This did it!
Dim newWidth = 500
Dim newHeight = 500
Clipboard.SetImage(New Bitmap(pbxDatasheet.Image, newWidth, newHeight))
rtbArmy.Paste()
Edit 2: It still doesn't work.
With the exact same code these things happen:
1. Most of the time the pasted picture(s) are the same size. But not the right size. I can change newWidth and newHeight all I want, they rarely differ in size. Although sometimes the picture is way larger than the others.
2. Sometimes it doesn't even add the picture to the RichTextBox. There's no pattern to when it happens.
The same lines of code can do all the above things. I have no idea what to do here.

Related

Problem changing size of a form on secondary display

I am creating a Windows form using VB.net
The form is part of a VSTO Excel Add-in
I move the form to a secondary display using:
Dim b As Rectangle = activeScreen.Bounds
Dim location As Point = Me.Location
Me.StartPosition = FormStartPosition.Manual
location.X = location.X + b.X
location.Y = location.Y + b.Y
Me.Location = location
where activeScreen is of type System.windows.forms.screen.
Before resizing, my form is size {Width = 1139 Height = 308}
I resize my form using
Me.size = newsize
where new size has value {Width = 2278 Height = 308}
However, the value of Me.size somehow jumps to {Width = 5695 Height = 770}
And on the screen, this is in fact what is seen, the form is now 2.5 times bigger than what I expect.
If I change the height of the form in this way, all works fine. Similarly, the above code works fine when I keep my form on the primary display.
The value of Me.DeviceDpi is 96 in both scenarios.
My primary display is scale 250% (recommended) and Display resolution 3200x1800 in the settings, and screen bounds is [Bounds={X=0,Y=0,Width=3200,Height=1800}
The secondary display is scale 100% (recommended) and Display resolution 1360x768 in the settings, and screen bounds is {X=8000,Y=938,Width=3400,Height=1920}
The answer to this question seems to bear no resemblance to the problem.
I was resizing my form as part of a response to Form_PreviewKeyDownevent
Moving the code to Form_KeyUp event, suddenly everything behaved correctly as it had done when running on the primary monitor.
In the same vain, part of my form contains a Label that I change and move dynamically. This form was simply disappearing when I move the form to the secondary display. Debugging size, location, bounds etc showed numbers consistent with all the other controls on the form. Tried calling BringToFront() thinking maybe some subtle scaling had caused the label to be moved behind another control, but this did not help either.
Solution / workaround - every time I change or move the label, remove it from the controls and re-add a newly created label. Once again, with this work around, all once again works like it did on the primary monitor.

vb.net- How to make any picture in a picture box round in shape

I am making an application in vb.net that has a good user interface, including the your account icon that includes a picture box. My question is that how can i make that image in the picture box round in shape?
Like This:
https://drive.google.com/open?id=1rUBq68ULDkTiFFv2uEiV_oQIh3wQIfhd
I wouldn't normally provide code for a question that doesn't show an attempt but there are a few steps in the answer and it was easier to just write the code myself than explain them. E.g.
'Get the original image.
Dim originalImage = PictureBox1.Image
'Create a new, blank image with the same dimensions.
Dim croppedImage As New Bitmap(originalImage.Width, originalImage.Height)
'Prepare to draw on the new image.
Using g = Graphics.FromImage(croppedImage)
Dim path As New GraphicsPath
'Create an ellipse that fills the image in both directions.
path.AddEllipse(0, 0, croppedImage.Width, croppedImage.Height)
Dim reg As New Region(path)
'Draw only within the specified ellipse.
g.Clip = reg
g.DrawImage(originalImage, Point.Empty)
End Using
'Display the new image.
PictureBox2.Image = croppedImage
That will create an elliptical image with the same width and height as the original. If the original is square then the final will be circular. If you want a circle regardless of the aspect ratio of the original then you'll have to manipulate that in the appropriate manner.
Though i see that you already found out the solution to your question, however here's a very simple workaround.
Assuming, your project is in WinForms, and you are using Picture Box control. The easiest way to achieve the round-image-look is to set the Image property of the PictureBox to an image that is round-shaped and is blank/transparent in the middle and white on the outside. Then , whatever image you set as a BackgroundImage , it will appear round-ish.
Here's a sample round-transparent image for you which you can set as Image(make sure to set SizeMode to stretch) and then set any picture as BackgroundImage.

Decrease space between controls in FlowLayoutPanel

How can I decrease the space between the controls even further?
I've set all the margins and padding to 0 but there is still space between my controlers.
this is the space I am getting with all margins and padding set to 0. I even set the margin and padding on each controller to 0.
and for the sake of consistency here is the code that is adding the PictureBoxes
Dim newPic As PictureBox = New PictureBox()
newPic.Image = p.Image
newPic.Size = New System.Drawing.Size(New Point(p.Size.Width * 2,
p.Size.Height * 2))
newPic.SizeMode = p.SizeMode
laytt.SetToolTip(newPic, ttstring)
AddHandler newPic.Click, AddressOf LayoutComponent_Clicked
LayoutFlowLayout.Controls.Add(newPic)
You are not setting the Margin property on the picture boxes you add. The default is 3,3,3,3. Add this line of code to fix the problem:
newPic.Margin = New Padding(0)
Every control handles margins differently, even with standard controls. Have a look at this example:
Notice that a Button reserves some space around it, while a TextBox takes everything. You may ask why 2 pixels in between them which you can clearly see. For that - please copy/paste into Paint and zoom in. Those 2 pixels are in fact the border, this is how a control is drawn. I am sure Buttons also have a border, but it's harder to justify visually, even when zoomed in.
If you want to change that, you would need to create a custom control and override how it's drawn, i.e. manually cut borders from it or similar. But I would not recommend doing it, to keep UI consistent.

Scrollable image in userform

I have a word doc with a bunch of ActiveX Control buttons or whatever on it, and each time a button is clicked, a corresponding image needs to be displayed in a popup box.
I have a userform called ImageForm, and this is what I'm doing right now:
Sub Button_Clicked()
ImageForm.Picture = LoadPicture("appropriate_image_path")
ImageForm.Show
End Sub
Each of these images has a width of 8.5 inches, but their heights can vary anywhere from like 3 to 20 inches (they're snippets of a pdf). So I've set the width of the userform to a little more than 8.5 inches, and that looks fine. But I need to be able to scroll vertically through the image in the userform, since some of the images could be taller than a user's monitor.
I'm completely stuck on this. What I've tried so far is adding a frame to the form, then adding an image control inside the form, and setting the "ScrollBars" property of the frame to vertical. Then instead of using "ImageForm.Picture = ..." I use "ImageForm.ImageControl.Picture = ..." But it doesn't work.
Any insight here would be greatly appreciated. Hopefully this question is clear enough, I've only been using VBA for a month or so now. (I miss Java so, so much)
Thanks!
Here is a neat little trick based on one of my posts
The idea is to ensure that the image control is in frame control and the image control doesn't have a border. Also the image control's PictureSizeMode is set to fmPictureSizeModeClip so that we can scroll the image
SNAPSHOT (DESIGN TIME)
SNAPSHOT (RUN TIME)
CODE
Private Sub UserForm_Initialize()
With Frame1
'~~> This will create a vertical scrollbar
.ScrollBars = fmScrollBarsVertical
'~~> Change the values of 2 as Per your requirements
.ScrollHeight = .InsideHeight * 2
.ScrollWidth = .InsideWidth * 9
End With
With Image1
.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg")
.BorderStyle = fmBorderStyleNone
.PictureSizeMode = fmPictureSizeModeClip
End With
End Sub

The control or subform control is too large for this location on resize

I have a simple form in Access 2003. The form has a List control and a button. I wanted to make the list to resize with the form (only vertically) while the button remains at the bottom right of the List. I'm using the following code on the form's resize event.
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50
This works fine as I resize the form, until the form height gets to a certain height. Then I get this error;
Run-time error '2100':
The control or subform control is too large for this location
This error is occurring at the line where I'm assigning the commandButton.Top. If I remove this line then the list height changes fine. I don't have any subforms in the form.
Does anyone know why this is happening?
Thanks
I think it is because you need to resize the detail section of the form first.
Try changing the code to
Me.Section(0).Height = Me.InsideHeight
list.Height = Me.InsideHeight - list.Top - 200
commandButton.Top = list.Height + list.Top + 50
Came by here (as many have) with the same problem and then realised my issue. Be mindful when resizing a control to a larger size with regard to the order of setting properties.
I would recommend setting the TOP and LEFT positions before HEIGHT and WIDTH.
Although my final sized control should have fitted OK once resized, I had originally tried setting the WIDTH first which attempted to enlarge the control exceeding the width of the form. My application threw the 2100 error at that point. I really hope this helps someone! Also, don't forget to set dimensions in TWIPS which is set as INCHESS x 1440 (or CM x 566.9291), ie: .Width = 10 * 566.9291 to set a control width to 10cm.
I get this same error if I set the width to greater the 31680.
With a little more research, I notice MS Access only allows a form width to be 22" wide. 22" = 31680 TWIPS.
So my workaround solutions it to add a check:
If newWidth > 31680 Then newWidth = 31680