简体   繁体   中英

Resizing and saving an image in c# that's downloaded from a remote URL

I have some code which downloads a file from a remote URL and saves it quite efficiently.

How can I modify this to set the height to be 120px (and width scale accordingly) when I save it?

    private static bool DownloadRemoteImageFile(string uri, string fileName)
    {


        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        HttpWebResponse response;
        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (Exception)
        {
            return false;
        }

        if ((response.StatusCode == HttpStatusCode.OK ||
            response.StatusCode == HttpStatusCode.Moved ||
            response.StatusCode == HttpStatusCode.Redirect) &&
            response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
        {

            using (Stream inputStream = response.GetResponseStream())
            using (Stream outputStream = System.IO.File.OpenWrite(fileName))
            {
                var buffer = new byte[4096];
                int bytesRead;
                do
                {
                    bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                    outputStream.Write(buffer, 0, bytesRead);
                } while (bytesRead != 0);
            }
            return true;
        }

        return false;
    }

The examples I've seen use the Bitmap class which I'm not using, do I need to change how I download my file?

Update your DownloadRemoteImageFile method to return a byte[] :

private static byte[] DownloadRemoteImageFile(string uri)
{
    byte[] content;
    var request = (HttpWebRequest)WebRequest.Create(uri);

    using (var response = request.GetResponse())
    using (var reader = new BinaryReader(response.GetResponseStream()))
    {
        content = reader.ReadBytes(100000);
    }

    return content;
}

Then use System.Drawing classes to re-size and save the image:

static void Main(string[] args)
{
    var img = DownloadRemoteImageFile( // Put your url here );

    Image original;

    using (var ms = new MemoryStream(img))
    {
        original = Image.FromStream(ms);
    }

    var newHeight = 120;
    var newWidth = ScaleWidth(original.Height, 120, original.Width);

    using (var newPic = new Bitmap(newWidth, newHeight))
    using (var gr = Graphics.FromImage(newPic))
    {
        gr.DrawImage(original, 0, 0, newWidth, newHeight);
        newPic.Save(@"C:\newImage1.jpg", ImageFormat.Jpeg);
    }
}

To scale the width:

private static int ScaleWidth(int originalHeight, int newHeight, int originalWidth)
{
    var scale = Convert.ToDouble(newHeight) / Convert.ToDouble(originalHeight);

    return Convert.ToInt32(originalWidth * scale);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM