2 Attachment(s)
[RESOLVED] CaptureControl CopyFromScreen is reducing the original image width and height
Hello, I hope someone out there can give me some help. What part of the program does is to take the original image and gets its image height and width and then flips the image over- much like having a physical photo in your hand and turning it over to see the backside. I figured out how to do that. I have been having problems saving the text to the image that is housed in the picturebox2, but I found a function from By Edgemeal on the forum here and it did the trick to save the image with text. The only thing is that when I use it, it reduces the size of the image. I need to find a way to maintain the original size being image width and height. Right now I start of with an image that is 640x400, I then flip the image over and the size is still 640x400, so far so good but, when I add the text to image and using the function I found to copyfromscreen it reduces the image to 474x367. I need some help here please.
I have a screen shot that I will upload as well so that you can see what it is doing. I am also using a combin image routine but that part is not the problem.
Attachment 125449
Code:
Private Sub ButAddTextBehindPic_Click(sender As System.Object, e As System.EventArgs) Handles ButAddTextBehindPic.Click
'Dim bimp As Bitmap
Dim drawfont As New Font(TextBox1.Font, 0)
Dim drawbrush As New SolidBrush(TextBox1.ForeColor)
Dim txt = TextBox1.Text
PictureBox2.CreateGraphics.DrawString(txt, drawfont, drawbrush, 10, 100)
'Clipboard.SetImage(PictureBox2.Image) this will not work for what I want.....just blank white image
' this one works does save image with text but reduse the width and height of image
bimp = CaptureControl(PictureBox2) ' capture pic2box area
bimp.Save(picsave3, Imaging.ImageFormat.Jpeg) ' save to file
PictureBox2.Image = bimp ' display in pic box
'PictureBox2.Image = ScaleImage(Image.FromFile(picsave3), PictureBox2.Height, PictureBox2.Width) this has no effect :-(
TextImgAfter.Text = ClientSize.Height ' shows 637 but on hard disk shows 367 for height
TextImageafterAdTxt.Text = ClientSize.Width ' shows 1267 (sometimes 1157) but on hard disk shows 474 for width
Dim dpiX As Single = PictureBox1.Image.HorizontalResolution
Dim dpiY As Single = PictureBox1.Image.VerticalResolution
TextHorzDPIafter.Text = dpiX
TextVertDPIafter.Text = dpiY
End Sub
Edit part of post:
the function I found:
Private Shared Function CaptureControl(ByVal control As Control) As Bitmap
Dim bmp As Bitmap = New Bitmap(control.ClientSize.Width, control.ClientSize.Height)
Using gr As Graphics = Graphics.FromImage(bmp)
gr.CopyFromScreen(control.PointToScreen(control.ClientRectangle.Location), Point.Empty, control.ClientSize)
End Using
Return bmp
End Function
Attachment 125451
The one on the left side is the original in picturebox1 the right is after using the function to capture the image. The original shows no DPI
Re: CaptureControl CopyFromScreen is reducing the original image width and height
I'm guessing that your PictureBox is 474x367.
If you load a picture (e.g. 320x200) into a PictureBox it will scale to fit into the PictureBox size (depending on the SizeMode property). If you save that image (PictureBox1.Image.Save), it will be 320x200. But if you load a 320x200 picture into a 150x100 PictureBox and copy from the screen, it will be the same size as the PictureBox, since that's what you copied.
Personally, I dislike anything that does a screenshot. The reason being that given you know the location/size of every control and the content of all of them, making a copy of the form can readily be done in Graphics (and you get a high quality output too).
Re: CaptureControl CopyFromScreen is reducing the original image width and height
I also wouldn't copy from screen.
It seems if you wanted to add text to a picture, there is a number of ways to do it, depending on if you want the text to be easily removed, so maintained separately from the image, or not.
Assuming the Not case, if you just loaded the picture into a bitmap, and then assign that bitmap to your picturebox, you can get a graphics object to access that bitmap and draw your text on it. A simple invalidate of the picturebox will update the picturebox so you see the modified bitmap in your picturebox.
The bitmap will always be full sized, regardless of what size it is displayed in the picturebox.
1 Attachment(s)
Re: CaptureControl CopyFromScreen is reducing the original image width and height
Quote:
Originally Posted by
Bulldog
I'm guessing that your PictureBox is 474x367.
If you load a picture (e.g. 320x200) into a PictureBox it will scale to fit into the PictureBox size (depending on the SizeMode property). If you save that image (PictureBox1.Image.Save), it will be 320x200. But if you load a 320x200 picture into a 150x100 PictureBox and copy from the screen, it will be the same size as the PictureBox, since that's what you copied.
Personally, I dislike anything that does a screenshot. The reason being that given you know the location/size of every control and the content of all of them, making a copy of the form can readily be done in Graphics (and you get a high quality output too).
Ok so that is one of the big problems thanks for clearing that up for me. So, I am using your suggestion:
So rewrote the code but now the problem is that it is removing the white background image and reverting to the control color.
Attachment 125453
Code:
Private Sub ButAddTextBehindPic_Click(sender As System.Object, e As System.EventArgs) Handles ButAddTextBehindPic.Click
'Dim bimp As Bitmap
Dim bmp = Bitmap.FromFile(picsave3)
Dim drawfont As New Font(TextBox1.Font, 0)
Dim drawbrush As New SolidBrush(TextBox1.ForeColor)
Dim txt = TextBox1.Text
Dim newImage1 = New Bitmap(bmp.Width, bmp.Height)
Dim grph = Graphics.FromImage(newImage1)
grph.DrawString(txt, drawfont, drawbrush, 10, 100)
'bmp.Dispose() Does not matter if disposed or not same outcome
'PictureBox2.Image.Dispose()
' image with text show in picbox
PictureBox2.Image = newImage1
On Error Resume Next
'bmp.Save(picsave3, Imaging.ImageFormat.Jpeg)
TextImgAfter.Text = PictureBox2.Image.Height
TextImageafterAdTxt.Text = PictureBox2.Image.Width
Dim dpiX As Single = PictureBox1.Image.HorizontalResolution
Dim dpiY As Single = PictureBox1.Image.VerticalResolution
TextHorzDPIafter.Text = dpiX
TextVertDPIafter.Text = dpiY
End Sub
Re: CaptureControl CopyFromScreen is reducing the original image width and height
The bitmap is transparent, so the only thing that is not transparent is the text you wrote on it, which is convenient if you wanted to draw the image with text on another image.
If you set PictureBox2's background color to White, then the transparent bitmap with text would allow that white to show.
If you don't want the text to be drawn on a transparent bitmap, either draw a white filled rectangle in the bitmap after you create it, or use the Clear method and pass the color white to it, so it clears the bitmap to white.
1 Attachment(s)
Re: CaptureControl CopyFromScreen is reducing the original image width and height
Thank you guys for pointing me in the write direction. It is working now like it should. The text is being saved and the dimension of image is being retained. thanks Bulldog and passel.
Attachment 125455
Now I just need to remember how to mark this resolved.