• Log in or sign up for an account here: https://www.sythe.org/login

Different uses of the WebClient.

Flaming Idiots

Active Member

Visual Basic Programmers
Joined
Dec 22, 2005
Sythe Gold
0
Messages
235
Age
36
Location
Alberta Canada
Two Factor Authentication User
The web client control has more uses than just getting strings or downloading files off the web.
It can also be used to get fonts and images off the web without ever saving them for example.
You could maybe get the raw data for a zip file and use .NETs zip unpacker (.NET 3.0 only) to make a faster updater for a program.

For this example make a Windows Forms application, and add a picture box.

What this code will do is download this sites banner and draw some text on it with some fonts you probably don't have.
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '' Loading images from the web.
        Dim Wc As New Net.WebClient()
        Dim Mem As New IO.MemoryStream(Wc.DownloadData("http://sythe.org/sythebanner.png"))
        Dim RawBitm As New Bitmap(Mem)
        Mem.Close()
        Mem.Dispose()

        '' Loading fonts from the web.
        Dim Font1 As Byte() = Wc.DownloadData("http://james.rictec.ca/visitor2.ttf")
        Dim Font2 As Byte() = Wc.DownloadData("http://james.rictec.ca/ka1.ttf")
        Dim FC As New System.Drawing.Text.PrivateFontCollection()
        For Each D As Byte() In New Byte()() {Font1, Font2}
            Dim Data As IntPtr = Runtime.InteropServices.Marshal.AllocCoTaskMem(D.Length)
            Runtime.InteropServices.Marshal.Copy(D, 0, Data, D.Length)
            FC.AddMemoryFont(Data, D.Length)
            Runtime.InteropServices.Marshal.FreeCoTaskMem(Data)
        Next


        Wc.Dispose()

        '' Displaying the downloaded image and writing with the font.
        Dim Bitm As New Bitmap(RawBitm.Width, RawBitm.Height, Imaging.PixelFormat.Format32bppArgb)
        Using G As Graphics = Graphics.FromImage(Bitm)
            G.DrawImage(RawBitm, New Rectangle(New Point(), RawBitm.Size))
            RawBitm.Dispose()
            Dim Q As New Drawing2D.GraphicsPath
            Dim scaling As Single = 2
            G.SmoothingMode = Drawing2D.SmoothingMode.None
            Q.AddString("Example of drawing text from downloaded fonts.", FC.Families(1), 0, CSng(12.5 * scaling), New Point(), StringFormat.GenericDefault)
            Q.AddString("Example of drawing text from downloaded fonts.", FC.Families(0), 0, CSng(5 * scaling), New Point(0, 40), StringFormat.GenericDefault)
            G.DrawPath(New Pen(Color.Black, 2 * scaling), Q)
            G.FillPath(Brushes.Black, Q)
            G.FillPath(Brushes.White, Q)
        End Using
        Me.PictureBox1.Image = Bitm

    End Sub
Now you can only draw on items with any downloaded fonts. You cannot set your forms font them, as they aren't registered font on the computer.

But the result should be like this, except without compression (If the fonts are wrong, or the program crashes, my web server is screwing up:mad:) :
View attachment Example.jpg
 
Runescape Gamblingrunescape gambling
Back
Top