4AspNet IPInfo v1.0

CountryInfo.GetFlagImageStream Method

Returns country flag image data.

Overload List

Returns country flag image data.

public Stream GetFlagImageStream();

Returns country flag image data.

public Stream GetFlagImageStream(bool);

Remarks

The returned Stream object contains sequence of bytes of the country flag thumbnail image. The format of an image is: PNG, 18x16 pixels. After obtaining this stream it is possible to save it's data to a file, write data to a response stream of a Web Forms, assign as an image for a PictureBox Windows Forms control or use data in some other way.

The first version of this method is always returns valid Stream object, even flag image is absent. In this case the stream with empty (transparent) image is returned.

The second version of method acts like first version if parameter is set to true, but returns null reference (Nothing in Visual Basic) if parameter is set to false and flag image is absent. This behavior allows to determine whether CountryInfo provides actual flag image for a country.

Example

The following example demonstares how to use flag image in the Windows Forms.

public void AssignFlagImageForPictureBox(string countryCode,
    System.Windows.Forms.PictureBox pictureBox, bool useCustomNAImage)
{
    // use full name of ForAspNet.IPInfo.License class; otherwise the compile-time
    // error will occure due to ambiguous reference
    ForAspNet.IPInfo.License.LicenseKey = "<put license string here>";
    
    IPInfo ipi = new IPInfo();
    CountryInfo ci = ipi.GetInfoByCountryCode(countryCode);
    System.IO.Stream flagStream;
    
    if(useCustomNAImage)
    {
        flagStream = ci.GetFlagImageStream(false);
        if(flagStream == null) // flag information is absent
        {
            // obtain custom image that indicates what flag is not available
            // from some other resource (i.e. file, database, etc).
            // ...
            // flagStream = ...;
        }
    }
    else
    {
        // get actual flag image or transparent image if flag information is absent
        flagStream = ci.GetFlagImageStream();
    }
    
    pictureBox.Image = System.Drawing.Image.FromStream(flagStream);
    flagStream.Close();
}

See Also

CountryInfo Class | ForAspNet.IPInfo Namespace