首页 > 解决方案 > asp.net文件上传控件上传外部图片URL

问题描述

我正在调用一个返回图像 URL 的外部 API,我想在收到 API 的响应后立即将该图像上传到文件上传控件,并且在该用户可以裁剪并调整图像然后保存它之后。

如果有人以前做过类似的事情,请帮助我。

标签: asp.netfile-uploadwebforms

解决方案


我以前做过:

使用..

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

下载,裁剪+调整图像大小:

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);

其他库编码(我个人收集的一些成像代码)

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);
    }
}

推荐阅读