How can I convert images to gif types real gif types images using memory stream ?

I’m downloading the images as jpg’s but even if I will try to download them as .gif it will not be gif really gif.
I will have to open the images in paint and save them one by one as gif type.

I want in the script in the completed even to convert the array of images to gif’s. To do it after this line :

images = Directory.GetFiles(@"d:\satimages", "*.jpg");

To convert them to gif’s without losing quality.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;
using unfreez_wrapper;

namespace SatImages
{
    public partial class Form1 : Form
    {
        private int counter = 0;
        private System.Windows.Forms.Timer moveTimer;
        private string[] images;

        public Form1()
        {
            InitializeComponent();

            moveTimer = new System.Windows.Forms.Timer();

            progressBar1.Value = 0;
            progressBar1.Maximum = 10;

            backgroundWorker1.RunWorkerAsync();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Download()
        {
            var wc = new WebClient();
            wc.BaseAddress = "https://en.sat24.com/en/tu/infraPolair";
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

            var temp = wc.DownloadData("/en");
            doc.Load(new MemoryStream(temp));

            var secTokenScript = doc.DocumentNode.Descendants()
                .Where(e =>
                       String.Compare(e.Name, "script", true) == 0 &&
                       String.Compare(e.ParentNode.Name, "div", true) == 0 &&
                       e.InnerText.Length > 0 &&
                       e.InnerText.Trim().StartsWith("var region")
                      ).FirstOrDefault().InnerText;
            var securityToken = secTokenScript;
            securityToken = securityToken.Substring(0, securityToken.IndexOf("arrayImageTimes.push"));
            securityToken = secTokenScript.Substring(securityToken.Length).Replace("arrayImageTimes.push('", "").Replace("')", "");
            var dates = securityToken.Trim().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            var scriptDates = dates.Select(x => new ScriptDate { DateString = x });
            foreach (var date in scriptDates)
            {
                string img = "https://en.sat24.com/image?type=infraPolair&region=tu&timestamp=" + date.DateString;

                Image image = DownloadImageFromUrl(img);
                image.Save(@"d:\satimages\" + counter + ".jpg");

                counter++;
            }
        }

        public class ScriptDate
        {
            public string DateString { get; set; }
            public int Year
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(0, 4));
                }
            }
            public int Month
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(4, 2));
                }
            }
            public int Day
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(6, 2));
                }
            }
            public int Hours
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(8, 2));
                }
            }
            public int Minutes
            {
                get
                {
                    return Convert.ToInt32(this.DateString.Substring(10, 2));
                }
            }
        }

        public System.Drawing.Image DownloadImageFromUrl(string imageUrl)
        {
            System.Drawing.Image image = null;

            try
            {
                System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl);
                webRequest.AllowWriteStreamBuffering = true;
                webRequest.Timeout = 30000;

                System.Net.WebResponse webResponse = webRequest.GetResponse();

                System.IO.Stream stream = webResponse.GetResponseStream();

                image = System.Drawing.Image.FromStream(stream);

                webResponse.Close();
            }
            catch (Exception ex)
            {
                return null;
            }

            return image;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Download();
            backgroundWorker1.ReportProgress(counter);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            images = Directory.GetFiles(@"d:\satimages", "*.jpg");
            unfreez_wrapper.UnFreezWrapper uw = new UnFreezWrapper();
            uw.MakeGIF(images.ToList(), @"d:\satimages\anim.gif", 8, true);

            moveTimer.Interval = 1000;
            moveTimer.Tick += new EventHandler(moveTimer_Tick);
            moveTimer.Start();
        }


        int imgc = 0;
        private void moveTimer_Tick(object sender, System.EventArgs e)
        {
            var image = Image.FromFile(images[imgc]);
            pictureBox1.Image = image;

            if (imgc < images.Length - 1)
            {
                imgc = imgc + 1;
            }
            else
            {
                imgc = 0;
            }
        }
    }
}

Well, first of all most what you have said doesn’t make much sense. You don’t download things “as something”. When you download something that something has a certain format. You can not download something as something else. If the image is a jpeg file you will download a jpeg. The extension of the file is irrelevant, the format it is saved in is important.

I have never used that “UnFreezWrapper” class, so I can’t tell if you’re using it right or not. Apart from that

makes no sense since gif is an indexed image format and only supports a 256 color palette. So you can only have 256 individual colors which is much more limited to a normal 8 bit per RGB channel image which supports up to 16 million colors. You can’t put an image which potentially supports 16 million colors into a format that only supports 256 colors without loosing quality, unless the image really only uses that few colors. Though since your source is a jpg you will have a lot of jpeg interpolation noise anyways.

Finally your code is a .NET forms application and not in anyways related to the Unity game engine. So I’m wondering why you ask a question in this forum if it’s not about game / application development with the Unity engine…