Button Background image won't shrink in VB.Net - vb.net

I used the following code in a VB.Net forms application:
Dim btnShowHideSize As New Size(30, 30)
btnShowHide.Size = btnShowHideSize
Dim img As New Bitmap(Resources.change2)
Dim img2 As New Bitmap(img, btnShowHideSize)
btnShowHide.BackgroundImage = img2
btnShowHide.BackgroundImageLayout = ImageLayout.Stretch
The image will expand, but not shrink. When I step through, I see that img size is 80,80 and img2 size is 30,30. Howewver, the image is not shrinking - it is getting cut off. If I go above 80,80 e.g. 200,200, the image does expand.
Is shrinking an image not possible?

Related

PDFsharp bitmap size

I'm using PDFsharp library to transform a control into a PDF file. The problem is that the bitmap image, even if I set height and width and the format is A4, does not fill the view in Adobe Reader.
I also tried to set the parameters manually from DPI but it does not work. The script is:
Dim docum As New PdfDocument()
For i As Integer = 0 To FoglioFattura1.listaPagIndex.Count - 1
Dim memImage = New Bitmap(FoglioFattura1.Width, FoglioFattura1.Height) 'A4Size
FoglioFattura1.DrawToBitmap(memImage, New Rectangle(0, 0, FoglioFattura1.Width, FoglioFattura1.Height))
Dim oPage As New PdfPage()
oPage.Orientation = PageOrientation.Portrait
oPage.Size = PageSize.A4
'oPage.TrimMargins.Left = 30
'oPage.TrimMargins.Top = 50
docum.Pages.Add(oPage)
Dim xgr As XGraphics = XGraphics.FromPdfPage(oPage)
Dim img As XImage = XImage.FromGdiPlusImage(memImage)
xgr.DrawImage(img, 0, 0)
Next
These are the dimensions of the document, I have hidden the content for privacy reasons. I want the document fill the view.
The problem is that you call DrawImage without specifying the destination size of the image in the PDF. Add Width and Height to the DrawImage call and the image will be drawn in the size you specify.
PDFs have no DPI. The size if the PDF page is given in Points - and Points are not pixels.
See also:
https://en.wikipedia.org/wiki/Point_(typography)

Disappearing Picturebox images [duplicate]

This question already has answers here:
Drawing Graphics Disappear in VB.net
(4 answers)
Closed 5 years ago.
I was successful in loading an image from a file into a picturebox in visual basic. Or so i thought. I had a msgbox in the code and the picture showed up in the picturebox. take the msgbox out and no picture. Any Ideas? Thanks
Private Sub DrawImageinSquarePanel(Panelname As PictureBox, ImageFile As String)
g = Panelname.CreateGraphics() 'creates new graphics element in panel
Dim newImage As Image = Image.FromFile(ImageFile) ' Create image.
Dim SquareDim As Integer 'Size of longest dimension in source image
If newImage.Width > newImage.Height Then
SquareDim = newImage.Width
Else
SquareDim = newImage.Height
End If
MsgBox(Panelname.Width & " " & Panelname.Height) 'the magic msgbox!!
' scale factor
Dim imageAttr As New ImageAttributes
imageAttr.SetGamma(Panelname.Width / SquareDim)
Dim ScaleFactor As Single = Panelname.Width / SquareDim
' Create rectangle for source and destination image.
Dim srcRect As New Rectangle(0, 0, newImage.Width, newImage.Height)
Dim destRect As New Rectangle((Panelname.Width - newImage.Width * ScaleFactor) / 2, (Panelname.Height - newImage.Height * ScaleFactor) / 2, newImage.Width * ScaleFactor, newImage.Height * ScaleFactor)
Dim units As GraphicsUnit = GraphicsUnit.Pixel
' Draw image to screen.
g.DrawImage(newImage, destRect, srcRect, units)
End Sub
Your title says "Picturebox" but there's no PictureBox there at all. If you want to display an Image in a PictureBox then assign the Image object to the Image property of that PictureBox.
Don't use GDI+ to draw it on a Panel and, if you ever do use GDI+ to draw, NEVER call CreateGraphics. Always draw on a control in its Paint event handler. The reason that your drawing disappears is that all drawing is erased on every Paint event. By doing your drawing in the Paint event handler, you ensure that it gets reinstated each time.
If you want to modify the image before displaying it then what you should do is create a new Bitmap object, use GDI+ to draw the modified image onto that and then assign that to the Image property of the PictureBox, e.g.
Using originalImage = Image.FromFile(filePath)
Dim newImage As New Bitmap(originalImage.Width, originalImage.Height)
Using g = Graphics.FromImage(newImage)
g.DrawImage(originalImage, Point.Empty)
End Using
'Dispose the existing image if there is one.
PictureBox1.Image?.Dispose()
PictureBox1.Image = newImage
End Using

Bitmap Returns Blank Image

I have a picturebox as the main image and it has a filled rectangle drawn onto it. Then I have another picturebox which has an image and a label which has some text. I want to draw the second picturebox's image and the label's text onto the first picturebox's image and display it in a third picturebox.
I have the following code:
Dim BMP As New Bitmap(PicBanner.Image)
Dim g As Graphics = Graphics.FromImage(BMP)
Dim Brsh As New SolidBrush(Color.FromName(CBForeClr.SelectedItem))
g.DrawImageUnscaled(PicLogo.Image, PicLogo.Location.X, PicLogo.Location.Y)
g.DrawString(LblText.Text, LblText.Font, Brsh, LblText.Left, LblText.Top)
PicBanner.DrawToBitmap(BMP, New Rectangle(0, 0, PicPreview.Width, PicPreview.Height))
PicPreview.Image = BMP
Instead of drawing the picturebox's image and the text onto the first picturebox, it displays only the filled rectangle in the preview box.
What is wrong with my code?

Draw an image partially from bottom to top

I have the code below to draw a partial image according to the value given in numericalUpDown control:
'Creates a new Bitmap from an existing image
Dim cards As New Bitmap(My.Resources.bar)
' Draws the rectangle
Dim srcRect As New Rectangle(0, 0, 22, NumericUpDown1.Value)
' Clones the partial image from the original image
Dim card As Bitmap = DirectCast(cards.Clone(srcRect, cards.PixelFormat), Bitmap)
'Locates the new image into the picturebox
PictureBox1.Image = card
It starts painting from top to down, but I want to start showing the image partials from bottom to top.
Any ideas?

Larger Image to fit in picturebox

As you'll can see the first image is the size of (1024*768) and it is correctly displayed in the picturebox and in the second case the image size is (1600*900) and it is displayed to half of the picturebox and the remaining is missing .So No I would like to fir that image in the picturebox no matter what the size is and even though it is greater than the size of the picturebox.I need to scale that Image.So how do I do that?And one more thing is that I need to resize the picturebox automatically when the image loads to it just as we see in the lightbox effect..
http://www.lokeshdhakar.com/projects/lightbox2/ -------->example.
Any help will be appreciated!
Here is what I am getting.
If it's a winforms app, you can set the SizeMode property of the PictureBox to PictureBoxSizeMode.Zoom. Note that this will increase the size of smaller images to fill the frame, so you might want to measure the image first, in order to check if either edge is too long, and then setting SizeMode to either PictureBoxSizeMode.Zoom or PictureBoxSizeMode.Normal.
I know this is marked answered, but I wrote this for one of my own apps. Hope it helps somebody..
Private Sub ScaleImage(ByVal p As PictureBox, ByRef i As Bitmap)
If i.Height > p.Height Then
Dim diff As Integer = i.Height - p.Height
Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
i = Resized
End If
If i.Width > p.Width Then
Dim diff As Integer = i.Width - p.Width
Dim Resized As Bitmap = New Bitmap(i, New Size(i.Width - diff, i.Height - diff))
i = Resized
End If
End Sub
The two Easiest Ways to Fit an Image to Any Size of PictureBox is:
-to set the Image as Background Image
OR
-to set it as picturebox image then set sizemode to StretchImage
1.Background Image
-use the BackgroundImage property of the PictureBox
picturebox.BackgroundImage = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
-Then Set its BackgroundImageLayout to stretch Like This:
picturebox.BackgroundImageLayout = ImageLayout.Stretch
Image
-use the Image property of the PictureBox
picturebox.Image = Image.FromStream(New IO.MemoryStream(CType(data, Byte())))
-Then Set its' sizeMode to StretchImage Like This:
picturebox.SizeMode = PictureBoxSizeMode.StretchImage
This will make any Picture / Image / Canvas Stroke (converted to Byte Array) fit according to the height and width of the picturebox
Hope This Helps :)