I want to capture the image of my webbrowser, or actually just a part of it.
I already achieved it, but it just takes a screenshot of it instead of capturing the bitmap itself.
So when i use this technique and you drag another window above it, the window is also captured. (this is what I don't want.)
My code:
' 1. Get the WebBrowsers bitmap.
Dim bmp As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
Dim BMPEndResult As Bitmap
Dim graph As Graphics = Graphics.FromImage(bmp)
Dim p As Point = Me.PointToScreen(WebBrowser1.Location)
graph.CopyFromScreen(p.X, p.Y, 0, 0, bmp.Size)
picDest.Image = bmp
So I need to get the image of the webbrowser:
- without the scrollbars if possible
- Even if the window is minimized or in the task bar
- Full webbrowser
- and also if possible just a part of it, where I can specify the top, left, width and height
WebBrowser.DrawToBitmap(bitmap, rectangle)
Related
I want to take a screenshot of a RichTextBox using the following code.
The problem is it takes a screenshot of another part of the Form:
Dim memoryImage As Bitmap
Dim myGraphics As Graphics = Me.CreateGraphics()
Dim s As Size = RichTextBox2.Size
memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
memoryGraphics.CopyFromScreen(RichTextBox2.Bounds.X, RichTextBox2.Bounds.Y, 0, 0, s)
memoryImage.Save(audiooutputfolder & name & ".png")
Graphics.CopyFromScreen() requires that you specify screen coordinates.
You can transform local coordinates into screen coordinates using the Control.RectangleToScreen() and Control.PointToScreen() methods.
Other methods do the opposite, see the Docs.
To compute the client area of a Control in screen coordinates, you can use its RectangleToScreen() method and pass the value of the ClientRectangle property:
Dim clientRectToScreen = [Control].RectangleToScreen([Control].ClientRectangle)
To include the non-client area (e.g., the borders of a Control, including the Scrollbars, if any), you need the screen coordinates of its Bounds.
There are different ways to do this. A simple method is to ask the Parent of a Control to get them, passing to the Parent's RectangleToScreen() method the Bounds of a child Control.
If you want to print a Form, which is a Top-Level Control, so it has no Parent, just use its Bounds directly: these measures already express screen coordinates.
It's shown in the ControlToBitmap() method:
Private Function ControlToBitmap(ctrl As Control, clientAreaOnly As Boolean) As Bitmap
If ctrl Is Nothing Then Return Nothing
Dim rect As Rectangle
If clientAreaOnly Then
rect = ctrl.RectangleToScreen(ctrl.ClientRectangle)
Else
rect = If(ctrl.Parent Is Nothing, ctrl.Bounds, ctrl.Parent.RectangleToScreen(ctrl.Bounds))
End If
Dim img As New Bitmap(rect.Width, rect.Height)
Using g As Graphics = Graphics.FromImage(img)
g.CopyFromScreen(rect.Location, Point.Empty, img.Size)
End Using
Return img
End Function
To take a screenshot of a Control, call this method, passing the Control you want to print to a Bitmap and specify whether you just want its content (the client area) or you want to include the non-client area (for example, if the control to print is a Form, you want to include the Caption and borders).
Important: use Path.Combine() to build a path:
Path.Combine(audiooutputfolder, $"{imageName}.png"
if string interpolation is not available ($"{variable} other parts"), you can glue the file extension to the file name:
Path.Combine(audiooutputfolder, imageName & ".png")
' Get the screenshot, client area only
Dim controlImage = ControlToBitmap(RichTextBox2, True)
' Save the image to the specified Path using the default PNG format
controlImage.Save(Path.Combine(audiooutputfolder, $"{imageName}.png"), ImageFormat.Png)
' [...] when done with the bitmap
controlImage.Dispose()
Side note:
If your app is not DpiAware, you may get wrong screen coordinates.
See these notes about this.
I have added an image to my iTextSharp PDF document like this:
Public Sub CreatePDFFromBitmap(ByVal uPath As String, ByVal uBitmap As Bitmap)
Dim nFs As System.IO.FileStream = New FileStream(uPath, FileMode.Create)
Dim nDocument As iTextSharp.text.Document
Dim nWriter As iTextSharp.text.pdf.PdfWriter
Dim nCb As iTextSharp.text.pdf.PdfContentByte
Dim nImgFromBitmap As System.Drawing.Image = DirectCast(uBitmap, System.Drawing.Image)
Dim nImg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(nImgFromBitmap, Imaging.ImageFormat.Png)
Dim bLandscape As Boolean = (nImg.Width > nImg.Height)
'rotation needs to be set before document is being opened
If bLandscape Then
nDocument = New iTextSharp.text.Document(PageSize.A4.Rotate, 0, 0, 0, 0)
Else
nDocument = New iTextSharp.text.Document(PageSize.A4, 0, 0, 0, 0)
End If
'if an exception is raised here, the following will help: https://stackoverflow.com/questions/15833285/pdfwriter-getinstance-throws-system-nullreferenceexception
nWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(nDocument, nFs)
nDocument.Open()
nCb = nWriter.DirectContent
nImg.ScaleToFit(nDocument.PageSize.Width, nDocument.PageSize.Height) 'raises dpi size :-)))
'X-Y-Koordinatensystem 0,0 startet also unten links, nicht oben-links
nImg.SetAbsolutePosition(0, nDocument.PageSize.Height - nImg.ScaledHeight)
nCb.AddImage(nImg)
nDocument.Close()
nWriter.Close()
nFs.Close()
End Sub
It works fine.
However, when I click the image in the PDF, it gets selected.
This is not what I want.
If I click the image in the PDF, it should not be selected.
This is what it looks like: The image becomes blue:
I want to add editable fields to the PDF, so I need to make the image not selectable, else it would confuse the user.
As Abdel-Rahman Al-Qawasmi mentions in his answer, it is completely up to the PDF viewer which entities it makes selectable and which not. Thus, there is no guaranteed way to get what you want.
Nonetheless, there are ways to put an image into a PDF which dissuade current versions of most PDF viewers from making it selectable. These ways either transform the bitmap image into a non-bitmap entity (e.g. by iterating over the pixels of the bitmap and drawing a little rectangle per pixel using vector graphics) or wrap the bitmap image into something that usually is not selectable.
Let's take the latter approach and wrap the image into a page-size PDF pattern with which we then fill the actual page. You can do that by replacing your
nCb.AddImage(nImg)
by
Dim painter As iTextSharp.text.pdf.PdfPatternPainter = nCb.CreatePattern(nDocument.PageSize.Width, nDocument.PageSize.Height)
painter.AddImage(nImg)
nCb.SetColorFill(New iTextSharp.text.pdf.PatternColor(painter))
nCb.Rectangle(0, 0, nDocument.PageSize.Width, nDocument.PageSize.Height)
nCb.Fill()
(This essentially is the VB/iTextSharp pendant of the Java/iText code from this answer.)
This is a pdf program specifications and not related to asp.net or vb.net programming. you need to have control of the pdf reader settings. Or try to use another format.
I have a panel which draws a diagram based on user input. Unfortunately the diagrams can get really really big, and when I print them it doesn't entirely fit on a single page without losing readability.
I'm using PDFsharp within VB.net to create the PDF. The current method is the turn the panel in which the diagram is drawn on to a bitmap image, and then place the image in the pdf document.
1 diagram can easily make the panel 1500,3000 in size.
Example of Diagram
Example of Diagram in PDF
Any help is greatly appreciated
Dim pdfdoc As New PdfDocument
Dim page As PdfPage = pdfdoc.AddPage
Dim Bitmap As Bitmap = New Bitmap(pnl_Draw.Width, pnl_Draw.Height)
Dim BXImage As XImage
Dim GFX As XGraphics
Me.pnl_Draw.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Height))
Dim pbx As New PictureBox
pbx.Image = Bitmap
BXImage = XImage.FromGdiPlusImage(pbx.Image)
GFX = XGraphics.FromPdfPage(page)
GFX.ScaleTransform(0.82)
GFX.DrawImage(BXImage, 0, 0)
GFX.Dispose()
pdfdoc.Save("G:\test.pdf")
pdfdoc.Close()
Try
Process.Start("G:\test.pdf")
Catch ex As Exception
End Try
Simple solution: in the Adobe Reader Print dialog click on "Poster" to print the image on more than 1 page. Increase the Zoom factor to distribute the poster on as many pages as you need.
You can also increase the page size proportionally to the bitmap size.
BTW: You don't need GFX.ScaleTransform(0.82) if you specify the destination size in the call to GFX.DrawImage.
Hi I'm trying to get a rectangle that completely surrounds a shape drawn via GDI+ when the pen is thick.
This is my test code to demo which I just added to mybase.load to test out..
Dim bmp As New Bitmap(500, 500)
Dim g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.Transparent)
g.SmoothingMode = SmoothingMode.HighQuality
g.PixelOffsetMode = PixelOffsetMode.Default
' Setup Matrix
Dim mat As New Matrix
mat.Translate(100, 100)
g.Transform = mat
' Draw Lines and Fill
Dim gp As New GraphicsPath
gp.AddLines({New Point(50, 0), New Point(100, 100), New Point(0, 100)})
gp.CloseAllFigures() ' Closes Open Path (ie join back to origin)
g.FillPath(New SolidBrush(Color.Beige), gp)
' Add Border to Graphics Path
g.DrawPath(New Pen(New SolidBrush(Color.Black), 20), gp)
g.DrawRectangle(New Pen(New SolidBrush(Color.Red), 1), Rectangle.Truncate(gp.GetBounds))
' Tidy Up
g.ResetTransform()
mat.Dispose()
' Set Picturebox Value
PictureBox1.Image = bmp
I'd post the image but the forum doesn't let me.
I want the bounding rectangle to include the pen width too. I used the above triangle to demo that I cant just add penwidth/2.
Any ideas?
Julian
If that is sufficient for your needs you can simply set the pen's .Alignment property to Inset like so:
g.DrawPath(New Pen(New SolidBrush(Color.Black), 20) With {.Alignment = PenAlignment.Inset}, gp)
This basically tells the pen to draw itself on the inside of the path at all times. Default is that it draws itself on the middle which leads to the situation that the outside width at edges is dependant on the angle of the edge.
Result is:
Just in case anyone else struggles like i did trying to find a solution then this is what I did in the end. I gave up trying to do using some form of getbounds method.
My solution in the was to scan the rendered bitmap instead to calculate the bounds based on pixels that were non-transparent (the background of my rendered image(s)). In fact I only need to scan once on the combined image and it takes a fraction of a second to calculate the "true" bounds now.
I used this article Fit a PictureBox to its non-transparent pixels to achieve a starting point that proved the concept and gave me a region that I could do a "getbounds" on. Then I swapped out the slow getpixel method for the lockbits example at Work with bitmaps faster in C# and voila!
Just ran all the code through an online c# to vb convertor and pasted it together to get completely working in less than an hour.
Took a fraction of the time I wasted trying to find a solution on the internet that would work directly on the vector image.
Julian
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 :)