Ich habe eine Web-Anwendung mit asp.net mvc4 und Rasierer entwickelt. In meiner Anwendung gibt es ein Steuerelement zum Hochladen von Dateien, um ein Bild hochzuladen und an einem temporären Speicherort zu speichern.Wie Größe ändern und speichern ein Bild, hochgeladen mit Datei-Upload-Steuerelement in C#
vor dem Speichern Bild sollte auf eine bestimmte Größe angepasst werden und dann in den temporären Speicherort gespeichert werden.
Hier ist der Code, den ich in der Controller-Klasse verwendet habe.
public class FileUploadController : Controller
{
//
// GET: /FileUpload/
public ActionResult Index()
{
return View();
}
public ActionResult FileUpload()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string relativePath = "~/img/" + Path.GetFileName(uploadFile.FileName);
string physicalPath = Server.MapPath(relativePath);
FileUploadModel.ResizeAndSave(relativePath, uploadFile.FileName, uploadFile.InputStream, uploadFile.ContentLength, true);
return View((object)relativePath);
}
return View();
}
}
und hier ist der Code in Modellklasse verwendet
public class FileUploadModel
{
[Required]
public HttpPostedFileWrapper ImageUploaded { get; set; }
public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
{
int newWidth;
int newHeight;
Image image = Image.FromStream(imageBuffer);
int oldWidth = image.Width;
int oldHeight = image.Height;
Bitmap newImage;
if (makeItSquare)
{
int smallerSide = oldWidth >= oldHeight ? oldHeight : oldWidth;
double coeficient = maxSideSize/(double)smallerSide;
newWidth = Convert.ToInt32(coeficient * oldWidth);
newHeight = Convert.ToInt32(coeficient * oldHeight);
Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
int cropX = (newWidth - maxSideSize)/2;
int cropY = (newHeight - maxSideSize)/2;
newImage = new Bitmap(maxSideSize, maxSideSize);
Graphics tempGraphic = Graphics.FromImage(newImage);
tempGraphic.SmoothingMode = SmoothingMode.AntiAlias;
tempGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
tempGraphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
tempGraphic.DrawImage(tempImage, new Rectangle(0, 0, maxSideSize, maxSideSize), cropX, cropY, maxSideSize, maxSideSize, GraphicsUnit.Pixel);
}
else
{
int maxSide = oldWidth >= oldHeight ? oldWidth : oldHeight;
if (maxSide > maxSideSize)
{
double coeficient = maxSideSize/(double)maxSide;
newWidth = Convert.ToInt32(coeficient * oldWidth);
newHeight = Convert.ToInt32(coeficient * oldHeight);
}
else
{
newWidth = oldWidth;
newHeight = oldHeight;
}
newImage = new Bitmap(image, newWidth, newHeight);
}
newImage.Save(savePath + fileName + ".jpg", ImageFormat.Jpeg);
image.Dispose();
newImage.Dispose();
}
}
, aber wenn ich die Anwendung ausführen tritt es ein Argument.
es sagt „Parameter nicht gültig ist“ in folgenden Codezeile
Bitmap tempImage = new Bitmap(image, newWidth, newHeight);
Wie kann ich hier gültig und entsprechende Parameter übergeben
public static void ResizeAndSave(string savePath, string fileName, Stream imageBuffer, int maxSideSize, bool makeItSquare)
Einfach genial! Vielen Dank! – MRFerocius
Hallo .. vielen Dank für das Hochladen der Post.aber Ihr Code funktioniert, wenn die Bildgröße unter 1 MB ist..wenn ich 3 MB oder 2 MB Bildgröße hochzuladen, gibt es Fehlermeldung "Nicht genügend Arbeitsspeicher." Können Sie bitte helfen ich ... – user3217843
Danke! Genau das, wonach ich gesucht habe. –