Post: [VB.NET/TUT] Build your own screensnapr
01-03-2012, 08:33 PM #1
fill0botto95
You talkin to me?
(adsbygoogle = window.adsbygoogle || []).push({}); Hi guys! This tutorial is not so hard but it's useful to learn something more about visual basic.

ScreenSnapR
- Capture the screen
- Upload the image
- Copy the link in the clipboard

Start with the Imports:
    Imports System.Drawing.Imaging, System.Net


This function generate a random string that will be the image file name:
    Public Shared Function Random(ByVal PasswordLength As Integer) As String        Dim _allowedChars As String = "abcdefghijkmnopqrstuvwxyz"
Dim randNum As New Random()
Dim chars(PasswordLength - 1) As Char
Dim allowedCharCount As Integer = _allowedChars.Length
For i As Integer = 0 To PasswordLength - 1
chars(i) = _allowedChars.Chars(CInt(Fix((_allowedChars.Length) * randNum.NextDouble())))
Next i
Return New String(chars)
End Function


To call it, use this sintax:
    Random(number of chars of the string)


Add also this function, the jpeg codec: (credits to unknown)
    Private Function MyCodec() As ImageCodecInfo        Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
For Each codec As ImageCodecInfo In codecs
If (codec.MimeType = "image/jpeg") Then
Return codec
End If
Next
Return Nothing
End Function


Let's pretend that the screen will be captured when button1 is pressed:

In this part it capture thet full screen:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.Sleep(250)
Dim myEncoder As New EncoderParameters(1)
myEncoder.Param(0) = New EncoderParameter(Encoder.Quality, 1000)
Dim bmp As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
g.Dispose()


Here generate the image file name:
    Dim rand As String = Random(Cool Man (aka Tustin)


Here set the path where it will save the image:
    Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & rand & ".jpg"

I used a common folder because we are going to delete the file after uploading.

Here it saves the image in the selected "Path" using our codec "MyCodec":
    bmp.Save(Path, MyCodec, myEncoder)


Now it upload the image. It sends the image to server and it will be uploaded via FTP so you need a website like altervista or 000webhost.
Let's "dim" a webrequest to our site using "ftp://" instead "https://" and add the path where the image will be located using the image file name
            Dim UploadRequest As System.Net.FtpWebRequest = _
DirectCast(System.Net.WebRequest.Create("ftp://www.screencapture.altervista.org/" [COLOR=#00ff00]& rand & ".jpg"[/COLOR]), System.Net.FtpWebRequest)



We declare the credential to access to our website via FTP:
    UploadRequest.Credentials = New System.Net.NetworkCredential("username", "password")


We set the method to send the file via FTP:
    UploadRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile


We "dim" the stream that will upload the image:
    Dim UploadStream As System.IO.Stream = UploadRequest.GetRequestStream()


It starts sending the image to the server via FTP:
    UploadStream.Write(System.IO.File.ReadAllBytes(Path), 0, System.IO.File.ReadAllBytes(Path).Length)


Set the direct link to the image as the clipboard text:
    Clipboard.SetText("www.screencapture.altervista.org/" & rand & ".jpg")


Delete the temporary image file:
    System.IO.File.Delete(Path)
End Sub
(adsbygoogle = window.adsbygoogle || []).push({});

The following 2 users say thank you to fill0botto95 for this useful post:

Correy, tokzikate
01-03-2012, 10:58 PM #2
tokzikate
Gym leader
Great in-depth tutorial, all it needs is some hotkeys :p

Originally posted by fill0botto95 View Post
Hi guys! This tutorial is not so hard but it's useful to learn something more about visual basic.

ScreenSnapR
- Capture the screen
- Upload the image
- Copy the link in the clipboard

Start with the Imports:
    Imports System.Drawing.Imaging, System.Net


This function generate a random string that will be the image file name:
    Public Shared Function Random(ByVal PasswordLength As Integer) As String        Dim _allowedChars As String = "abcdefghijkmnopqrstuvwxyz"
Dim randNum As New Random()
Dim chars(PasswordLength - 1) As Char
Dim allowedCharCount As Integer = _allowedChars.Length
For i As Integer = 0 To PasswordLength - 1
chars(i) = _allowedChars.Chars(CInt(Fix((_allowedChars.Length) * randNum.NextDouble())))
Next i
Return New String(chars)
End Function


To call it, use this sintax:
    Random(number of chars of the string)


Add also this function, the jpeg codec: (credits to unknown)
    Private Function MyCodec() As ImageCodecInfo        Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
For Each codec As ImageCodecInfo In codecs
If (codec.MimeType = "image/jpeg") Then
Return codec
End If
Next
Return Nothing
End Function


Let's pretend that the screen will be captured when button1 is pressed:

In this part it capture thet full screen:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Threading.Thread.Sleep(250)
Dim myEncoder As New EncoderParameters(1)
myEncoder.Param(0) = New EncoderParameter(Encoder.Quality, 1000)
Dim bmp As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
g.Dispose()


Here generate the image file name:
    Dim rand As String = Random(Cool Man (aka Tustin)


Here set the path where it will save the image:
    Dim Path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & rand & ".jpg"

I used a common folder because we are going to delete the file after uploading.

Here it saves the image in the selected "Path" using our codec "MyCodec":
    bmp.Save(Path, MyCodec, myEncoder)


Now it upload the image. It sends the image to server and it will be uploaded via FTP so you need a website like altervista or 000webhost.
Let's "dim" a webrequest to our site using "ftp://" instead "https://" and add the path where the image will be located using the image file name
            Dim UploadRequest As System.Net.FtpWebRequest = _
DirectCast(System.Net.WebRequest.Create("ftp://www.screencapture.altervista.org/" [COLOR=#00ff00]& rand & ".jpg"[/COLOR]), System.Net.FtpWebRequest)



We declare the credential to access to our website via FTP:
    UploadRequest.Credentials = New System.Net.NetworkCredential("username", "password")


We set the method to send the file via FTP:
    UploadRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile


We "dim" the stream that will upload the image:
    Dim UploadStream As System.IO.Stream = UploadRequest.GetRequestStream()


It starts sending the image to the server via FTP:
    UploadStream.Write(System.IO.File.ReadAllBytes(Path), 0, System.IO.File.ReadAllBytes(Path).Length)


Set the direct link to the image as the clipboard text:
    Clipboard.SetText("www.screencapture.altervista.org/" & rand & ".jpg")


Delete the temporary image file:
    System.IO.File.Delete(Path)
End Sub
01-11-2012, 06:45 AM #3
sexy as I might say.

put in hotkeys for the noobs. Or I will. thanks..

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo