Different uses of the WebClient.

Discussion in 'Programming General' started by Flaming Idiots, Feb 27, 2007.

Different uses of the WebClient.
  1. Unread #1 - Feb 27, 2007 at 4:42 AM
  2. Flaming Idiots
    Joined:
    Dec 22, 2005
    Posts:
    235
    Referrals:
    1
    Sythe Gold:
    0
    Two Factor Authentication User

    Flaming Idiots Active Member
    Visual Basic Programmers

    Different uses of the WebClient.

    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
     
< [video] How to make a google search EASY | My crappy OCR >

Users viewing this thread
1 guest


 
 
Adblock breaks this site