picture's backgroundcolor in picturebox - vb.net

how can i change the background color of a picture saving from a picturebox in vb.net.In my form there is a drawing section.after drawing i am saving the picture as jpeg.but the image's background color is black.so i can't see anything that i have drawn.the drawing pen color is also black.if anyone knows please help me.thank you.

I would say that the easiest way is to paint the background when the image object is created (sample initializing the background to being white):
Dim theImage As Image = New Bitmap(someWidth, someHeight)
Using g As Graphics = Graphics.FromImage(theImage)
g.Clear(Color.White)
End Using

To use this the following code:
OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"
OpenFileDialog1.ShowDialog()
Textbox1.Text = OpenFileDialog1.FileName
PicText.Image = Image.FromFile(OpenFileDialog1.FileName)

Related

How to obtain original size of image viewed in Windows.Forms PictureBox.Image

I want to know size of a image in a PictureBox, but cannot to access the Image property, because it's nothing.
Dim PiBox1 As New PictureBox
With PiBox1
.ImageLocation = "image.png"
.SizeMode = PictureBoxSizeMode.Zoom
.Dock = DockStyle.fill
If .Image Is Nothing Then MsgBox("Image = Nothing")
End With
PictureBox.Size, PictureBox.ClientSize return dimensions of the control, not the image inside...
While there are other options, to change the code as little as possible, just add a call to the Load method after setting the ImageLocation property. That will force the Image to be loaded synchronously.
That said, you really ought to be setting the Dock and SizeMode properties first. Why would you load an Image first and then change the size afterwards? Do all the configuration first, then load the Image as the last step. This is akin to setting the DataSource last when data-binding.

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?

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 :)

Saving drawn images in a PictureBox

I created a form with a PictureBox. I created a drawing in the PictureBox. But I can't save the file to a specified folder without dialogue controls. Only through code. Please help me if any one knows a solution for this problem. Thank you....
if you drawing in picture box you must convert drawing in bmp format with this code:
'Save as BMP-file
Dim bmp As New Bitmap(picturebox1.Width, picturebox1.Height)
draw.DrawToBitmap(bmp, New Rectangle(0, 0, picturebox1.Width, picturebox1.Height))
bmp.Save("D:\output.png", Imaging.ImageFormat.Png)
bmp.Dispose()
with this code you can print & save drawing on picturebox object
PictureBox has an Image property of type System.Drawing.Image which you use to get or set the image that the PictureBox represents. System.Drawing.Image has a Save method which will write the image to the specified filename or stream. Is this information OK or do you need more; your question was a little unclear.