Click here to Skip to main content
15,890,557 members
Articles / Desktop Programming / Win32
Tip/Trick

.Gif viewer / Snipper Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Jun 2015CPOL1 min read 18K   917   5  
C# .Gif viewer / Snipper control

Introduction

I create simple Gif/snipper tools. It's useful for most programs.

It is difficult to load and animate a picture .Gif within Windows form application. With this tool, you can easily do it just by giving image URL to this tool display animation. Also, you can select any .Gif photos to implement your spinner.

Background

After writing many programs, I found out that many people have problems with viewing Gif images, because Gif image cannot load in form thread. In C#, every object has its own thread and when we load .gif image in our project, the .gif image does not animate, then I create a simple tool for loading .gif image in forms.

Image 1

Using the Code

That coding is simple, only add control ".dll" to your project. You need to right click on the toolbox, select "Choose Items.." and browse to the DLL containing the control or go to project menu > add reference and and browse to the DLL, then rebuild your project and then drag and drop control from toolbox on your form.

Image 2

You can select your image with "LoadGIFImage" in property window.

Image 3

or use code:

C#
//
// show image and start moving
//
Spinner.Start();
// invisibale image and stop moving
//
Spinner.Stop();
//property
Spinner.LoadGIFImage = // load gif image

How It Works

In C#, every object has its own thread and when we load .gif image in our project, the .gif image does not animate. For this reason, we use BackgroundWorker to create a new thread for our .gif image.

C#
public Image LoadGIFImage
{
    get { return Loading_pb.Image; }
    set { Loading_pb.Image = value; }

public void Start()
{
    IsStart = true;
    this.Visible = true;
    var worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(Loading);
    worker.RunWorkerAsync();
}

public void Stop()
{
    IsStart = false;
    this.Visible = false;
}

void Loading(object sender, DoWorkEventArgs e)
{
    while (IsStart)
    {
        this.Refresh();
    }
}

History

  • 27th June, 2015 - Initial release

Thank you

Thanks for reading and I hope you like the control.

If you make any modifications/bug fixes/enhancements to this control, please post in the comments section with your source snippets and/or ideas.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --