asp.net - asp.net文件上传控件上传外部图片URL
问题描述
我正在调用一个返回图像 URL 的外部 API,我想在收到 API 的响应后立即将该图像上传到文件上传控件,并且在该用户可以裁剪并调整图像然后保存它之后。
如果有人以前做过类似的事情,请帮助我。
解决方案
我以前做过:
使用..
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);
}
}
推荐阅读
- angular - BehaviorSubject 返回初始值,尽管它在 Angular 中被更改
- go - 无阻塞以太网捕获
- c - 程序重新启动功能
- sql - SQL JOINS - 3 个表
- tensorflow - RNN 给出错误 ValueError:一个操作对梯度有 `None`
- google-chrome - CSP 设置使用 ERR_HTTP2_PROTOCOL_ERROR 杀死 Chrome 中的站点
- qt - 解析相同标记名下的 xml 节点子值
- reactjs - 反应显示 TreeNode
- php - 如何从 Angular 中的 php API(laravel 流明)获取数据数组
- python - 我收到一个 __initerror