GDI 中发生一般错误;
A 有一个图像上传功能,可以在 localhost 上正常工作,但是当我尝试在 Windows Server 2003 下运行时,我收到错误消息“
This is the code..”,
现在在有人攻击我之前;)我已经查看了之前的答案,并且我已检查所有权限,它们似乎是正确的..文件夹/路径存在,等等。
ImageService imageService = new ImageService();
if (fileBase != null && fileBase.ContentLength > 0 && fileBase.ContentLength <= 2097152 && fileBase.ContentType.Contains("image/"))
{
var uploadedPath = "~/Assets/Images/";
Path.GetExtension(fileBase.ContentType);
var extension = Path.GetExtension(fileBase.FileName);
if (extension.ToLower() != ".jpg" && extension.ToLower() != ".gif") // only allow these types
{
photoViewModel.ImageValid = "Not Valid";
ModelState.AddModelError("Photo", "Wrong Image Type");
return View(photoViewModel);
}
EncoderParameters encodingParameters = new EncoderParameters(1);
encodingParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo jpgEncoder = imageService.GetEncoderInfo("image/jpeg");
var uploadedimage = Image.FromStream(fileBase.InputStream, true, true);
Bitmap originalImage = new Bitmap(uploadedimage);
Bitmap newImage = new Bitmap(originalImage, 274, 354);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
var streamLarge = new MemoryStream();
newImage.Save(streamLarge, jpgEncoder, encodingParameters);
var fileExtension = Path.GetExtension(extension);
string newname;
if (photoViewModel.photoURL != null)
{ newname = photoViewModel.photoURL; }
else
{ newname = Guid.NewGuid() + fileExtension; }
var ImageName = newname;
newImage.Save(Server.MapPath(uploadedPath) + ImageName);
System.IO.File.WriteAllBytes(Server.MapPath(uploadedPath) + ImageName, streamLarge.ToArray());
photoViewModel.uploadedPath = uploadedPath;
photoViewModel.photoURL = ImageName;
originalImage.Dispose();
newImage.Dispose();
streamLarge.Dispose();
return View(photoViewModel);
}
A have an image upload function that works fine on localhost but when I try and run under Windows Server 2003 I get the error message
This is the code..
Now before anyone jumps on me ;) I've looked at the previous answers and I've checked all permissions and they seem to be correct.. the folder/paths exist, etc..
ImageService imageService = new ImageService();
if (fileBase != null && fileBase.ContentLength > 0 && fileBase.ContentLength <= 2097152 && fileBase.ContentType.Contains("image/"))
{
var uploadedPath = "~/Assets/Images/";
Path.GetExtension(fileBase.ContentType);
var extension = Path.GetExtension(fileBase.FileName);
if (extension.ToLower() != ".jpg" && extension.ToLower() != ".gif") // only allow these types
{
photoViewModel.ImageValid = "Not Valid";
ModelState.AddModelError("Photo", "Wrong Image Type");
return View(photoViewModel);
}
EncoderParameters encodingParameters = new EncoderParameters(1);
encodingParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
ImageCodecInfo jpgEncoder = imageService.GetEncoderInfo("image/jpeg");
var uploadedimage = Image.FromStream(fileBase.InputStream, true, true);
Bitmap originalImage = new Bitmap(uploadedimage);
Bitmap newImage = new Bitmap(originalImage, 274, 354);
Graphics g = Graphics.FromImage(newImage);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
var streamLarge = new MemoryStream();
newImage.Save(streamLarge, jpgEncoder, encodingParameters);
var fileExtension = Path.GetExtension(extension);
string newname;
if (photoViewModel.photoURL != null)
{ newname = photoViewModel.photoURL; }
else
{ newname = Guid.NewGuid() + fileExtension; }
var ImageName = newname;
newImage.Save(Server.MapPath(uploadedPath) + ImageName);
System.IO.File.WriteAllBytes(Server.MapPath(uploadedPath) + ImageName, streamLarge.ToArray());
photoViewModel.uploadedPath = uploadedPath;
photoViewModel.photoURL = ImageName;
originalImage.Dispose();
newImage.Dispose();
streamLarge.Dispose();
return View(photoViewModel);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Image.FromStream
,流必须在图像的生命周期内保持打开状态。这意味着如果流是一个文件,该文件将保持打开状态。我认为您需要处置 uploadedImage 以允许关闭流(如果您等待 GC 清理 uploadedImage,这将在未来的不确定点发生 - 或者可能根本不会发生)。With
Image.FromStream
, the stream must remain open for the lifetime of the image. That means if the stream is a file, the file will be held open. I think you'll need to dispose uploadedImage to allow the stream to be closed (if you wait for the GC to clean up the uploadedImage, this will occur at an indeterminate point in the future - or may not occur at all).