0

I am calling an external API which return me URL of an image , I want to upload that image to file upload control as soon as I get response from API and after that user can crop and make adjustment to image and then save it.

Please help me if someone has done something like before.

1
  • 1
    I don't think you can upload a url. Research downloading an img from a url (or sim.). Commented Oct 23, 2019 at 5:30

1 Answer 1

1

I have done this before:

using..

using System.Net;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

download, crop+resize the image:

string imgurl = "https://seeklogo.net/wp-content/uploads/2015/10/stack-overflow-logo-vector-download.jpg";

// download the image
WebClient wc = new WebClient();
byte[] ba = wc.DownloadData(imgurl);

// convert it into image
MemoryStream ms = new MemoryStream(ba);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

// crop and resize
System.Drawing.Image img2 = ResizeCropFitToSize(img, 200, 80);

// convert it into byte[]
MemoryStream ms2 = new MemoryStream();
img2.Save(ms2, ImageFormat.Jpeg);
byte[] ba2 = ms2.ToArray();

// save the file
string filepath = Server.MapPath("~/logo.jpg");
File.WriteAllBytes(filepath, ba2);

other library coding (some of my personal collection of imaging codes)

public static System.Drawing.Image ResizeCropFitToSize(System.Drawing.Image fullSizeImage, int width, int height)
{
    System.Drawing.Image img = ResizeImageMinSize(fullSizeImage, width, height);
    System.Drawing.Image img2 = CropImageCenter(img, width, height);
    img.Dispose();
    return img2;
}

public static System.Drawing.Image ResizeImageMinSize(System.Drawing.Image FullsizeImage, int MinimumWidth, int MinimumHeight)
{
    // Prevent using images internal thumbnail
    FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
    FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

    int NewWidth = 0;
    int NewHeight = 0;

    NewHeight = MinimumWidth * FullsizeImage.Height / FullsizeImage.Width;
    NewWidth = MinimumWidth;
    if (NewHeight < MinimumHeight)
    {
        NewHeight = MinimumHeight;
        NewWidth = MinimumHeight * FullsizeImage.Width / FullsizeImage.Height;
    }

    System.Drawing.Image NewImage = (System.Drawing.Image)(new Bitmap(FullsizeImage, NewWidth, NewHeight));

    return NewImage;
}

public static System.Drawing.Image CropImageCenter(System.Drawing.Image image, int Width, int Height)
{
    int StartAtX = (image.Width - Width) / 2;
    int StartAtY = (image.Height - Height) / 2;

    return CropImage(image, StartAtX, StartAtY, Width, Height);
}

public static System.Drawing.Image CropImage(System.Drawing.Image image, int StartAtX, int StartAtY, int Width, int Height)
{
    System.Drawing.Image outimage;
    MemoryStream mm = null;
    try
    {
        //check the image height against our desired image height
        if (image.Height < Height)
        {
            Height = image.Height;
        }

        if (image.Width < Width)
        {
            Width = image.Width;
        }

        //create a bitmap window for cropping
        Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(72, 72);

        //create a new graphics object from our image and set properties
        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
        grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;

        //now do the crop
        grPhoto.DrawImage(image, new Rectangle(0, 0, Width, Height), StartAtX, StartAtY, Width, Height, GraphicsUnit.Pixel);

        // Save out to memory and get an image from it to send back out the method.
        mm = new MemoryStream();
        bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
        image.Dispose();
        bmPhoto.Dispose();
        grPhoto.Dispose();
        outimage = System.Drawing.Image.FromStream(mm);

        return outimage;
    }
    catch (Exception ex)
    {
        throw new Exception("Error cropping image, the error was: " + ex.Message);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.