Printscreen the screen and save the image(VB) - vb.net

Can someone help me, how can I printscreen my screen that can be saved in gif or jpeg format
locally in VB.net

I know this question was asked a long time ago so I post this for posterity.
It's quite trivial to preform a screen capture operation using the CopyFromScreen method in the Graphics class. Also, the BitMap class ships with a Save method; making this even more trivial.
Using image As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Using surface As Graphics = Graphics.FromImage(image)
surface.CopyFromScreen(Screen.PrimaryScreen.Bounds.Location, Point.Empty, image.Size)
End Using
image.Save("C:\myimage.jpg", Imaging.ImageFormat.Jpeg)
End Using
One possible solution when dealing with multiple monitors is to iterate, capture and save each screen as individual images. Place the above code inside the following For Each Next statement and replace Screen.PrimaryScreen with monitor. Be sure that you set a unique file name for each image.
For Each monitor As Screen In Screen.AllScreens
'...
Next

Related

How do I Resize An Image In a RichTextBox?

this might sound like a repost of a question already asked. However, I have spent a few days looking through and testing many different examples from a number of different websites (incl. stackoveflow.com). Up to now, I got mostly undesired results and am a bit lost as to how to achieve this or something which produces good results? If anyone is able to help I would greatly appreciate it!
I work in VB.NET and use Visual Studio 2015.
My objective is to get a crisp image in the resulting RTF file when I open it in a text editor (I use WPS Office). However, the low quality, out of focus image I have been getting in the final RTF document is not a good header for the description I want to use it for! Nor will it look good after printing! I am not looking for a thumbnail image as the image is required for a larger document header.
Most solutions I have tried deal with resizing an image and then pasting it into a Rich Text Box(RTB).
However, although I have tried using interpolation, antialiasing, etc., the results are not great. For resizing, I have tried examples which use division, percentage, etc.
I am working on an app for my own personal use that "pastes" a screenshot of an active solution/app (i.e. bitmap) into a Picture Box(PB) and into an RTB using the clipboard, title is appended then text added manually in the RTB and the result is saved as an RTF file.
Image Quality issues I have found:
Pasting an image after resizing/scaling the original (or a copy) in the Picture Box reduces quality?
Saving to an RTF file or opening the RTF file in a text editor reduces quality? (Not sure which causes the issue?)
Both these processes result in a low quality, out of focus image (with or without interpolation, smoothing, antialiasing, etc.)? So, it seems to me that resizing before pasting reduces quality. I have tried pasting directly from the clipboard and also copying the image from the Picture Box to clipboard, then pasting to RTB. Both these procedures produce good results!
To be objective, I cannot share all the code I have tested, but share below a simple example of code which produces the best image quality for me, i.e. just simply pasting the screenshot directly from the clipboard to the RTB. However, the resulting image after pasting into the RTB has not been resized and is therefore too big. I don't want to manually resize the image in the RTF document as this would be an extremely repetitive task!
After pasting the bitmap into the RTF, I have used the Select All or Selection Start - 1 methods to select the image in the RTB, but to frame my question, "How can I resize the image in the RTB?"
You might ask why I want to do this. Well, pasting the bitmap directly into the RTB gives me the same quality image as the one in the Picture Box. There is a slight loss after saving to an RTF file and opening it in a text editor, but the quality is much better than the other methods mentioned above (i.e. resize then paste)! So, I got curious to attempt to paste and then resize! Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Check whether PB1 is empty:
If PictureBox1.Image Is Nothing Then
'Get image from clipboard to PB1:
PictureBox1.Image = My.Computer.Clipboard.GetImage
'Resize image to fit PB1:
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
'Paste image:
RichTextBox1.Paste()
End Sub

TcpClientActivex capture to picturebox

Hello i'm trying to make an automated camera capture using TcpClientActivex control. I'm still a beginner at vb.net. But i don't know how to capture it then set the picturebox image.
I can use TcpClientActivex.GetImage.Save("location") though but i don't wanna save it into file, i want to set it to picturebox using codes without saving the file. I tried researching for it but the results are not what i was looking for...
i tried TcpClientActivex.GetImage.Save(picturebox1.image)
and
TcpClientActivex.GetImage.SaveAdd(picturebox1.image
but didn't work...
Did you try assigning the image to the picture box?
PictureBox1.Image = TcpClientActivex.GetImage()
Please read up on the Assignment Operator. You might also want to find yourself a tutorial or book to get a grasp of the basic concepts of VB.NET and Object-oriented programming.

Printing a form in vb.net

I wrote a small program to store and hopefully print contact information (in vb.net). The program contains a few text fields and a picture box. Saves the Information and so on. Can anyone recommend a quick way to save the form to a format that can then be transferred to her work computer and printed (she can't install my program at work, taboo policy and so on). Using the printform control, printform.print() with the PrintAction set to PrintToFile is just giving me garbled junk. I guess I could print to an html file bit by bit, but I thought I'd ask if anyone knows a better way. Also, with the html route I'm not sure how I'd add the contents of the picture box. Thanks in advance.
You can try saving your Form to a Bitmap using the DrawToBitmap Method which could then be saved as an image then printed out later, the main problem you will run into with this method is that the DPI settings are different between the screen and a printer.
Dim bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(bmp, New Rectangle(New Point(0, 0), Me.Size))
bmp.Save("C:\temp\123.bmp") 'Set your path and your filename here

VB.NET printing images using a lot of memory

I'm working with PDFs in VB.NET using a DLL I found on code project:
http://www.codeproject.com/Articles/37458/PDF-Viewer-Control-Without-Acrobat-Reader-Installe
My app allows you to select multiple files in a grid and print them. The files are stored in password-protected zip files, so the first step I do is extract each selected file to a memory stream that I pass to a new PDF wrapper object. Each object gets added to a queue. Then, each object in the queue is printed, page by page, as a system.drawing.image. The whole thing runs on a background worker.
Now, extracting the PDFs to the queue uses hardly any memory. But in the PrintPage event handler, when I extract the images and send them to the printer, something must be going wrong. My memory usage explodes. Each image, of course, is large because it's rendered at 300 dpi, but the memory used by each page isn't being returned to the OS and neither is it being garbage collected.
In the end, if I select enough files, I run out of memory. Why?
Ok, so I finally figured it out.
First, as far as the images go, the CLR apparently doesn't know how much memory is allocated for a Drawing.Image so when you dispose it, you have to tell it:
'It's 4 bytes per pixel with RGBA
'Use Drawing.Image.PixelFormat to get
'the number of bytes if you don't know
Dim countBytes as long = 4 * img.Width * img.Height
'Let the CLR know of the memory we want to free
if countBytes > 0 then GC.AddMemoryPressure(countBytes)
'Get rid of the image
img.Dispose()
img = Nothing
'Free up the unused memory
GC.Collect()
'Tell the CLR we took care of it
GC.RemoveMemoryPressure(countBytes)
Now, the PDF library from the CodeProject sample was quite a bit more difficult.
First of all, make sure you call the Dispose method on the PDFWrapper object in either the FormClosed event of the form that holds the wrapper, or in the Finalize method of the class that holds it.
But, the PDFWrapper actually seems to cache the images you retrieve from it. So as you page through a PDF, memory usage will grow until the images for entire PDF are cached. This is an even bigger problem if you use those images to print the PDF at 300DPI (I get out of memory errors toward the end of a 60+ page PDF at 1.5GB of memory used).
There is no 'Clear Cache' method for this object as far as I can tell. But the hack I used to get it working was to grab an image at 1DPI after I get the image I need, then perform garbage collection as above. This indirectly frees up the memory that was cached. However, like before, we must tell the CLR how many bytes we used. It's the same calculation as above.
BUT there is one more problem. The PDFWrapper object is actually grabbing the images on another thread, it seems. So, by requesting another 1DPI image after we request the 300DPI image, it gets confused and randomly spits out 1DPI images when it should be giving us 300DPI images to print. So, the workaround for this:
Dim img As System.Drawing.Image
img = AFPDFLibUtil.GetImageFromPDF(pdfWrapper, currentPage, DPI)
'Wait for PDFWrapper to finish rendering
Dim sw As New Stopwatch()
sw.Start()
While _pdfWrapper.IsBusy
If sw.ElapsedMilliseconds < TimeoutMS Then
System.Threading.Thread.Sleep(10)
Else
Throw New Exception("This page took too long to render.")
End If
End While
sw.Stop()
sw.Reset()
And there you go. Perhaps that's why in the CodeProject sample, he uses a different DLL to do the printing. However, the PDFWrapper object supports reading from a IO.MemoryStream, I don't think any of the other includes in that project do.
Happy coding to anyone who reads this!

picture is used by another process

in my windows application there is a image saving process.i can save different images.the images details will seen in a grid ,when i click the corresponding row in grid the image will shown in a picturebox.i want to delete the open picture by pressing the delete key.i used the
"deletefile(path)" code for this operation.but there is an error that "This file is used by another process."if anyone knows the solution for this problem please help me.thank you.
Do you have a reference to a Bitmap object created from that file ? If so, the Bitmap object is locking the file and will prevent you from deleting it.
The problem is not where you delete your file, it lies in how you open your image to display it. Could you maybe add some code showing how you load your image ?
When you load an image using something list Bitmap.FromFile, the Bitmap object keeps a lock on the file until it is disposed. So you could simply use the
using(Bitmap bmp = Bitmap.FromFile(path))
{
/* The code using the bitmap to display it goes here */
}
construct to force it to release the file once you do not need it. This will prevent it from locking. The reason it locks is that it does not load the whole bitmap in memory when you create the bitmap object, it loads it lazily on demand, so it needs to keep a lock on the file.
Open the imagefile using Image.FromStream and make sure you close the stream after the image is loaded. That way you sould have no locks on the file.
Added after comment.
I don't have Visual Studio at hand and I'm a c# guy but it should look something like this.
Dim stream As New FileStream(specified_path, FileMode.Open)
Dim image As Image = Image.FromStream(stream)
picturebox1.image = image
stream.Close()