Wednesday, December 15, 2010

WPF download and save image using VB.NET

Dim myWebClient As New WebClient()
        Dim strThumb As New Uri("http://www.somesite.com/image.jpg")
        Using stream As Stream = myWebClient.OpenRead(strThumb)
        
            Using bitmap As New System.Drawing.Bitmap(stream)
                'flush and close the stream
                stream.Flush()
                stream.Close()
            
                bitmap.Save(AppDomain.CurrentDomain.BaseDirectory + "Cache\test.jpg")
            End Using
        End Using

Monday, December 6, 2010

WPF Create Menu Icon On The Fly

I needed to retrieve a remote image url and use it as a menu item in my vb.net wpf application.

Did it as so where mnuTitle is the menu item to have the icon changed.
This should work with Silverlight as well.

Dim ico As New Image
 ico.Source = New BitmapImage(New Uri("http://www.site.com/image.jpg",UriKind.RelativeOrAbsolute))
 mnuTitle.Icon = ico

Friday, September 10, 2010

Install AjaxTookit In VS2010

As others I ran into problems with xml nodes showing up trying to use the toolkit controls. This occurred after installing vs2010 side by side with vs2008 and choosing to copy settings.

Here is the fix.
http://weblogs.asp.net/yousefjadallah/archive/2010/04/16/installing-ajax-control-toolkit-4-in-visual-studio-2010.aspx

Friday, September 3, 2010

Online Web Service Testing

Found a great site today.  I was working on retrieving xbox live gamer tags via a service over here through sivlerlight and was having some problems.

After having no luck I looked for a service repository hoping to find an alternate.

I found a great web service directory http://www.service-repository.com/ and indeed it has an online test included on the site.

Turns out the soap xbox webservice at http://duncanmackenzie.net/Blog/put-up-a-rest-api-for-xbox-gamertag-data
no longer works

Thursday, September 2, 2010

Misc Code: Two input form fields sharing SQL DB column

Ran into something new to me with ASP.net two form fields sharing the same column.


From my SqlDatasource
InsertCommand="INSERT INTO [tblRequest] ([reqMake], [reqModel], [reqCal], [reqEmail], [reqPrice], [reqType], [reqNotes], [reqAnswered]) VALUES (@reqMake, @reqModel, @reqCal, @reqEmail, @reqPrice, @reqType, @reqNotes,  @reqAnswered)"
Parameters.
<InsertParameters>
                                        <asp:ControlParameter ControlID="DropDownList2" Name="reqMake"
                                            PropertyName="SelectedValue" Type="String" />
                                        <asp:ControlParameter ControlID="DropDownList3" Name="reqModel"
                                            PropertyName="SelectedValue" Type="String" />
                                        <asp:ControlParameter ControlID="DropDownList1" Name="reqCal"
                                            PropertyName="SelectedValue" Type="String" />
                                        <asp:ControlParameter ControlID="txtEmail" Name="reqEmail" PropertyName="Text"
                                            Type="String" />
                                        <asp:ControlParameter ControlID="txtPrice" Name="reqPrice" PropertyName="Text"
                                            Type="String" />
                                        <asp:ControlParameter ControlID="DropDownList4" DefaultValue="" Name="reqType"
                                            PropertyName="SelectedValue" Type="String" />
                                        <asp:ControlParameter ControlID="txtRequestFireArmNotes" Name="reqNotes"
                                            PropertyName="Text" Type="String" />
                                        <asp:Parameter DefaultValue="False" Name="reqAnswered" Type="Boolean" />
                                      
                                         
                                    </InsertParameters>

It is corrected in the SQLDataSource Inserting event as such.
'Add second field here.
'Event start
e.Command.Parameters("reqMake").Value = txtExample1.Text + txtExample2.Text
'Event end

Read and Write compressed data to a binary file using ASP.NET

The BinaryReader and BinaryWriter in System.IO namespace read and write primitive types in binary, to a stream. The System.IO.Compression.GZipStream provides methods and properties to compress and decompress the stream. In this article, we will learn how to use compression techniques to compress the stream and write it to a binary file. We will then use the BinaryReader and the GZipStream class to decompress and read the file to display it on the webpage.
We have different types of  files on our computer. The Binary file is one such format. You cannot read these binary files by just opening them in the notepad. In this article, we will use the classes BinaryWriter and BinaryReader to add information to the file and to retrieve information from it. We will be using a FileStream constructor to open and modify the file if it already exists, or create a new file if it does not exist. You can then pass the FileStreamobject and use it to construct your BinaryReader and BinaryWriter. Let us see some code:
Read and Writing Binary Files Without Compression
Note: Use the namespaces System.IO
Writing Binary data
C#
string str = "Read and Write compressed data to a binary file”;
// Write to a binary file
        FileStream fs = null;
        BinaryWriter bw = null;
        try
        {
            fs = new FileStream(@"C:\test.bin", FileMode.OpenOrCreate);
            bw = new BinaryWriter(fs);
            bw.Write(str);          
        }
        catch (IOException ex)
        {
        }
        finally
        {
            bw.Flush();
            bw.Close();
            fs.Close();
        }
VB.NET
Dim str as string = "Read and Write compressed data to a binary file”
' Write to a binary file
            Dim fs As FileStream = Nothing
            Dim bw As BinaryWriter = Nothing
            Try
                  fs = New FileStream("C:\test.bin", FileMode.OpenOrCreate)
                  bw = New BinaryWriter(fs)
                  bw.Write(str)
            Catch ex As IOException
            Finally
                  bw.Flush()
                  bw.Close()
                  fs.Close()
            End Try
Reading Binary Data
C#
// Read from a binary file
        FileStream fs1 = null;
        BinaryReader br = null;
        try
        {
            fs1 = new FileStream(@"C:\test.bin", FileMode.Open);
            br = new BinaryReader(fs1);
            string str1;
            str1 = br.ReadString();
            Response.Write(str1);
        }
        catch (IOException ex)
        {
        }
        finally
        {
            br.Close();
            fs1.Close();
        }       
VB.NET
' Read from a binary file
            Dim fs1 As FileStream = Nothing
            Dim br As BinaryReader = Nothing
            Try
                  fs1 = New FileStream("C:\test.bin", FileMode.Open)
                  br = New BinaryReader(fs1)
                  Dim str1 As String
                  str1 = br.ReadString()
                  Response.Write(str1)
            Catch ex As IOException
            Finally
                  br.Close()
                  fs1.Close()
            End Try
Read and Writing Binary Files Using Compression
Note: Use the namespaces System.IO.Compression. Here instead of passing the file stream object directly to the BinaryWriter, we are passing the stream to GZipStream to compress it. The compressed stream is then passed to BinaryWriter. Similarly while reading the file, we pass the stream to GZipStream to decompress and then use BinaryReader to read the file and display its contents.
Writing Binary data
C#
// Compress and write to a binary file
        FileStream fs = null;
        BinaryWriter bw = null;
        try
        {
            fs = new FileStream(@"C:\test1.bin", FileMode.OpenOrCreate);
            GZipStream cmp = new GZipStream(fs, CompressionMode.Compress);
            bw = new BinaryWriter(cmp);
            bw.Write(str);
        }
        catch (IOException ex)
        {
        }
        finally
        {
            bw.Flush();           
            bw.Close();
            fs.Close();
        }
VB.NET
' Compress and write to a binary file
            Dim fs As FileStream = Nothing
            Dim bw As BinaryWriter = Nothing
            Try
                  fs = New FileStream("C:\test1.bin", FileMode.OpenOrCreate)
                  Dim cmp As GZipStream = New GZipStream(fs, CompressionMode.Compress)
                  bw = New BinaryWriter(cmp)
                  bw.Write(str)
            Catch ex As IOException
            Finally
                  bw.Flush()
                  bw.Close()
                  fs.Close()
            End Try
Reading Binary Data
C#
// Decompress and read from a binary file
        FileStream fs1 = null;
        BinaryReader br = null;
        try
        {
            fs1 = new FileStream(@"C:\test1.bin", FileMode.Open);
            GZipStream dcmp = new GZipStream(fs1,
                CompressionMode.Decompress);
            br = new BinaryReader(dcmp);
            string str1;
            str1 = br.ReadString();
            Response.Write(str1);
        }
        catch (IOException ex)
        {
        }
        finally
        {
            br.Close();
            fs1.Close();
        }  
VB.NET
' Decompress and read from a binary file
            Dim fs1 As FileStream = Nothing
            Dim br As BinaryReader = Nothing
            Try
                  fs1 = New FileStream("C:\test1.bin", FileMode.Open)
                  Dim dcmp As GZipStream = New GZipStream(fs1, CompressionMode.Decompress)
                  br = New BinaryReader(dcmp)
                  Dim str1 As String
                  str1 = br.ReadString()
                  Response.Write(str1)
            Catch ex As IOException
            Finally
                  br.Close()
                  fs1.Close()
            End Try
In this article, we saw how to use compression techniques to compress the stream and write it to a binary file. We then used the BinaryReader and the GZipStream class to decompress the stream and read the file to display it on the webpage. I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed.

Misc

Save