webForumDet fria alternativet

ändra storlek på bild vid upload

.NET

4 svar · 359 visningar · startad av dea

Medlem sedan dec. 2002387 inlägg
Frågan#1

ASP.Net & C#

Sitter med ett projekt där jag ska göra en sida där en "okunnig" person ska skicka bilder till en mapp i en server. Bilderna han ska uploada är oftast i storleken 1280 * 960. Genast där har jag problem, jag tycker det är lite stort, sen kan det hända att han skickar in en bild som är 1600 * 1200 och då blir det ännu jobbigare..

Finns det något sätt att ändra storleken när han trycker på Send knappen? Till 700 * 525 eller så..

Just nu har jag tre uploader som gör olika saker:

public class UploadForm : System.Web.UI.Page
	{
		public const int MaxWidth = 300;
		public const int MaxHeight = 300;

		public const int MinSize = 5 * 1024;
		public const int MaxSize = 20 * 1024;

		public const int ThumbnailWidth = 180;
		public const int ThumbnailHeight = 135;

		protected HtmlGenericControl message;

		protected HtmlInputFile file;
		protected HtmlInputFile file1;
		protected HtmlInputFile file2;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(IsPostBack)
			{
				message.Attributes.Add("align", "left");

				UploadImageWithSizeRestrictions(file.PostedFile);
				UploadImageWithDimensionRestrictions(file1.PostedFile);
				UploadImageWithNoRestrictions(file2.PostedFile);
			}
			else
			{
				message.Attributes.Add("align", "center");
				AppendMessage("Select file to upload.");
			}
		}

		private void AppendMessage(string msg)
		{
			message.Controls.Add(new LiteralControl(msg));
		}

		private void DisplayImageInfo(HttpPostedFile image)
		{
			AppendMessage(String.Format("FileName: {0}<br>", image.FileName));
			AppendMessage(String.Format("FileSize: {0} KB<br>",
				image.ContentLength / 1024));

			using(Bitmap bitmap = new Bitmap(image.InputStream, false))
			{
				AppendMessage(String.Format("Width: {0}<br>", bitmap.Width));
				AppendMessage(String.Format("Height: {0}<br>", bitmap.Height));

				if(bitmap.RawFormat.Equals(ImageFormat.Bmp))
				{
					AppendMessage("ImageFormat: Bitmap Image (BMP)<br>");
				}
				if(bitmap.RawFormat.Equals(ImageFormat.Gif))
				{
					AppendMessage("ImageFormat: Graphics Interchange Format (GIF)<br>");
				}
				if(bitmap.RawFormat.Equals(ImageFormat.Jpeg))
				{
					AppendMessage("ImageFormat: Joint Photographic Experts " +
						"Group (JPEG)<br>");
				}
				if(bitmap.RawFormat.Equals(ImageFormat.Png))
				{
					AppendMessage("ImageFormat: W3C Portable Network Graphics (PNG)<br>");
				}
			}
		}

		private bool IsImage(HttpPostedFile file)
		{
			if(file != null && Regex.IsMatch(file.ContentType, "image/\\S+") &&
				file.ContentLength > 0)
			{
				DisplayImageInfo(file);
				return true;
			}

			return false;
		}

		protected void UploadImageWithSizeRestrictions(HttpPostedFile file)
		{
			if(IsImage(file))
			{
				int contentLength = file.ContentLength;
				if(contentLength > MinSize && contentLength < MaxSize)
				{
					UploadImage(file);
				}
				else
				{
					AppendMessage(String.Format("<span style=\"color:red;\">Image size " +
						"exceeding limits: {0} KB</span><br><br>",
						contentLength / 1024));
				}
			}
		}

		protected void UploadImageWithDimensionRestrictions(HttpPostedFile file)
		{
			if(IsImage(file))
			{
				using(Bitmap bitmap = new Bitmap(file.InputStream, false))
				{
					if(bitmap.Width < MaxWidth && bitmap.Height < MaxHeight)
					{
						UploadImage(file);
					}
					else
					{
						AppendMessage(String.Format("<span style=\"color:red;\">Image " +
							"dimensions exceeding limits: {0} x {1}</span><br><br>",
							bitmap.Width, bitmap.Height));
					}
				}
			}
		}

		protected void UploadImageWithNoRestrictions(HttpPostedFile file)
		{
			if(IsImage(file))
			{
				UploadImage(file);
			}
		}

		protected void UploadImage(HttpPostedFile file)
		{
			string fileName =
				file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1);
			string filePath = MapPath("uploaded_images/" + fileName);

			file.SaveAs(filePath);
			AppendMessage("<span style=\"color:green;\">File Uploaded...</span><br>");

			using(System.Drawing.Image image =
					  System.Drawing.Image.FromStream(file.InputStream))
			using(Bitmap bitmap = new Bitmap(image, ThumbnailWidth, ThumbnailHeight))
			{
				bitmap.Save(MapPath("uploaded_images/t__" + fileName), image.RawFormat);
				AppendMessage("<span style=\"color:green;\">Thumbnail" +
					" created...</span><br><br>");
			}
		}

Tacksam för svar!

Medlem sedan mars 2003106 inlägg
#2

Jag har gjort en thumbnail-konverterare (JPG) utan kvalitetsförlust på följande sätt:

VB.NET------------------------------------------------------------------------------------------------------------
Dim TIA As System.Drawing.Image.GetThumbnailImageAbort
Dim imgFormat As Imaging.ImageFormat
Dim mImage As System.Drawing.Image

imgFormat = Imaging.ImageFormat.Jpeg

mImage = mImage.FromFile(Server.MapPath("minbild.jpg") )
Dim btm As New Bitmap(mImage)
mImage.Dispose()
mImage = btm
mImage = mImage.GetThumbnailImage(700, 525, TIA, IntPtr.Zero)
mImage.Save(Server.MapPath("minbild_tbn.jpg") , imgFormat)
------------------------------------------------------------------------------------------------------------

Medlem sedan apr. 20012 266 inlägg
#3

http://www.webforum.nu/showthread.php?s=&threadid=99806&forumid=191 innehåller även funktioner för att ändra storlek på bild.

Medlem sedan jan. 20012 204 inlägg
#4

xrobx skrev:

Jag har gjort en thumbnail-konverterare (JPG) utan kvalitetsförlust på följande sätt:

VB.NET------------------------------------------------------------------------------------------------------------
Dim TIA As System.Drawing.Image.GetThumbnailImageAbort
Dim imgFormat As Imaging.ImageFormat
Dim mImage As System.Drawing.Image

imgFormat = Imaging.ImageFormat.Jpeg

mImage = mImage.FromFile(Server.MapPath("minbild.jpg") )
Dim btm As New Bitmap(mImage)
mImage.Dispose()
mImage = btm
mImage = mImage.GetThumbnailImage(700, 525, TIA, IntPtr.Zero)
mImage.Save(Server.MapPath("minbild_tbn.jpg") , imgFormat)
------------------------------------------------------------------------------------------------------------

Hmm utan kvalitetsförlust? Tveksamnt va?

( http://www.webforum.nu/showthread.php?s=&postid=837068#post837068 )

Medlem sedan mars 2003106 inlägg
#5

Hmm utan kvalitetsförlust? Tveksamnt va?

Utam direkt kvalitetsförlust så länge du förminskar bilden och den
är i jpg-format redan från början :)

265 ms totalt · 4 externa anrop · v20260731065814-full.e96017d9
120 ms — deklarationer (db)
0 ms — hämta statistik (cache)
137 ms — hämta tråd, inlägg och bilagor (db)
125 ms — ändringar (db)