I am trying to convert a `BLOB` that is stored in a database to a `JPG` and then I need to display it on an `html form` in a `C#` application. 

A little backstory first - this is an older web app that was designed to take a `JPG` (photo) that was stored as a path in the database, resize it, then show as a `BLOB`. This functionality needs to stay in place, but now one of the systems is changing and the photo is going to be stored in the database as a `BLOB` instead of a path to a photo (\\servername\images\xyz.jpg). 

One system still needs the image to be resized then sent back as a `BLOB` to display. The other systems that call this web-app will need the `BLOB` converted to a `JPG` to display.

The code below works, but I think it might have put an unnecessary load on the server, because this morning the system was laggy and some features were impossible to use. I have a feeling that the `.Save` is the troublemaker here since I am calling it twice. I know the image doesn't need to be saved anywhere physically, as long as the web-app can call and have it display to the site. Is there anything I can change here to skip saving an image on the server and just display it so it doesn't use up a ton of resources?




    protected void Page_Load(object sender, EventArgs e)
    {
       byte[] byteImage = null;
        string ID = Request.QueryString["id"];
        string imageFile = "\\\\server\\c$\\inetpub\\wwwroot\\web-app\\images\\" + ID+ ".jpg";
        string server = Request.ServerVariables["server_name"];
        var serverNameList = db.Servers.Select(x => x.ServerName).ToList();

        connection.Open();
        SqlCommand sqlCommand = new SqlCommand("sp", connection);
        sqlCommand.Parameters.Add("@ID", SqlDbType.VarChar);
        sqlCommand.Parameters["@ID"].Value = ID;
        sqlCommand.CommandType = CommandType.StoredProcedure;
        SqlDataReader dr = sqlCommand.ExecuteReader();                               

        while (dr.Read())
        {
            byteImage = (byte[])dr["BLOB"];
        }

        if (byteImage != null)
        {
            using (MemoryStream ms = new MemoryStream(byteImage))
            {
                //saving to jpg image
                Image img = new Bitmap(ms);
                img.Save(imageFile, ImageFormat.Jpeg);

                Response.Clear();
                Response.CacheControl = "public";
                Response.Cache.SetExpires(DateTime.Now.AddDays(1.0));
                Response.ContentType = "Image/jpeg";
                   
                if (serverNameList.Contains(server)) // if user is coming from a specific server, resize the image then send back
                {
                    byte[] resizedImage = GetResizedImage(imageFile, 300, 400);
                    Response.OutputStream.Write(resizedImage, 0, resizedImage.Length);
                }
                else // skip resize and show image as jpg
                {
                    Response.BinaryWrite(byteImage);
                }
            }
        }
        else // image is null
        {
            imageFile = HttpRuntime.AppDomainAppPath + "PERSON.GIF";
        }         
    }
        
    private byte[] GetResizedImage(string path, int width, int height)
    {
        Bitmap bitmap1 = new Bitmap(path);
       double height1 = bitmap1.Height;
        double width1 = bitmap1.Width;
        double num = 1.0;
        if (width > 0)
            num = width / width1;
        else if (height > 0)
        num = height / height1;
        MemoryStream memoryStream = new MemoryStream();
        Bitmap bitmap2 = new Bitmap((int)(width1 * num), (int)(height1 * num));
        bitmap2.SetResolution(150f, 150f);
        Graphics graphics = Graphics.FromImage(bitmap2);
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.Clear(Color.White);
        graphics.DrawImage(bitmap1, new Rectangle(0, 0, (int)(num * width1), (int)(num * height1)), new Rectangle(0, 0, (int)width1, (int)height1), GraphicsUnit.Pixel);
        bitmap2.Save(memoryStream, ImageFormat.Jpeg);
        return memoryStream.ToArray();
    }