我在asp.net中输入了文件。我想获取图像,显示缩略图并保存。这是aspx ......
form id="form1" runat="server" enctype="multipart/form-data">
<div>
<input type="file" runat="server" />
<br />
<asp:Button runat="server" ID="btn_upload" OnClick="btn_upload_Click" Text="Upload" />
<br />
<asp:Image ID="image_placeholder" runat="server" Visible="false" />
</div>
</form>
这里是c#代码。我可以显示图像,我很难保存它。
var postedFile = httpRequest.Files[file];
string fileName = postedFile.FileName;
if (fileName != "")
{
Stream fs = postedFile.InputStream;
System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
{
using (MemoryStream memoryStream = new MemoryStream())
{
thumbnail.Save(memoryStream, ImageFormat.Png);
Byte[] bytes2 = new Byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes2, 0, (int)bytes2.Length);
string base64String = Convert.ToBase64String(bytes2, 0, bytes2.Length);
image_placeholder.ImageUrl = "data:image/png;base64," + base64String;
image_placeholder.Visible = true;
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
thumbnail.Save(desktopFolder, System.Drawing.Imaging.ImageFormat.Png);
}
}
我得到的错误是“GDI +中出现一般错误”
我认为这是因为我打电话给两个thumbnail.Save
无论如何,图像显示正常,实际保存它只是有问题。
答案 0 :(得分:1)
我最近不得不编写一个小应用程序来生成PDF文件的缩略图。我最终使用了一个名为Magick.NET的ImageMagick库。这个库让你很容易做到你所描述的。我使用方法MagickImage.Resize()
将图像大小调整为缩略图大小,使用属性MagickImage.Format
来设置缩略图的图像格式,使用方法MagickImage.Write()
将缩略图写入磁盘。您可能希望在https://magick.codeplex.com/处查看。
答案 1 :(得分:0)
在这里使用第三方库http://imageresizer.codeplex.com/.我努力上传图片并保留质量,这个图书馆帮了我很多。
在您看来:
@model YourProject.ViewModels.ProductImageUploadCreateViewModel
@using (Html.BeginForm("Upload", "ProductImage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="ImageFile1" id="ImageFile1">
}
你的控制器:
public class ProductImageController:Controller { [HttpPost] public ActionResult Upload(ProductImageUploadCreateViewModel viewModel) { if(!ModelState.IsValid) { return View(viewModel); }
if (viewModel.ImageFile1 != null)
{
UploadImage(viewModel.ProductId, "1", viewModel.ImageFile1);
}
// Return to where ever...
}
} 您的上传方式:
private void UploadImage(int productId,string imageNo,HttpPostedFileBase imageFile) { string uploadPath = Server.MapPath(“〜/ Assets / Images / Products /”+ productId); if(!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); }
Dictionary<string, string> versions = new Dictionary<string, string>();
versions.Add("_m", "width=150&height=150&scale=both&format=jpg"); // Medium size
string filePrefix = productId + "_" + imageNo;
versions.Add("_s", "width=90&height=90&scale=both&format=jpg"); // Small size
versions.Add("_l", "width=300&height=300&scale=both&format=jpg"); // Large size
foreach (string fileSuffix in versions.Keys)
{
// Generate a filename
string fileName = Path.Combine(uploadPath, filePrefix + fileSuffix);
// Let the image builder add the correct extension based on the output file type
fileName = ImageBuilder.Current.Build(imageFile, fileName, new ResizeSettings(versions[fileSuffix]), false, true);
}
} 看看他们的网站,你可以通过几个样本来完成。我希望这会有所帮助:)