hi There,
I am busy with a web application in C#. Customers will have the possibility to upload there company logo for there profile.
Uploading/resizing images is no problem butt when the images are gif or png with transparancy the image is really ugly after resizing. The transparancy gets black in stead of transparant.
I hope there is a default way to avoid this problem. I know there is a default way in php so maybe someone can help me on this one.
Thank already! (Sorry, the code comments are in Dutch language)
This is the code I use:
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace LibraPortal.UploadImage
{
public class UploadForm : System.Web.UI.Page
{
//Maximale hoogte van de thumbnail indien het in verhouding een hoog plaatje is
public const int ThumbnailSizeHigh = 60 ;
//Maximale breedte van het plaatje indien het in verhouding een breed plaatje is
public const int ThumbnailSizeWidth = 150 ;
//Maximale breedte van het plaatje indien het in verhouding een redelijk vierkant plaatje is
public const int ThumbnailSizeWidthSquare = 70;
//Calculatievariabelen
public double ThumbnailWidth ;
public double ThumbnailHeight;
public double ThumbnailRatio;
//Werkgever ID (Deze wordt meegenomen in de bestandsnaam om overschrijving te voorkomen)
public string EmpID;
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");
UploadImageWithNoRestrictions(file2.PostedFile);
}
}
private void AppendMessage(string msg)
{
message.Controls.Add(new LiteralControl(msg));
}
private void DisplayImageInfo(HttpPostedFile image)
{
//Javascripts die ervoor zorgen dat de juiste waardes naar het parent window worden verplaatst
//en het upload venster wordt gesloten.
AppendMessage(String.Format("<script language='javascript'>opener.setVeld1('{0}'); </script>", System.IO.Path.GetFileName(image.FileName)));
AppendMessage(String.Format("<script language='javascript'>window.close();</script>"));
}
private bool IsImage(HttpPostedFile file)
{
//Controleren of het geslecteerde bestand een plaatje is
if(file != null && Regex.IsMatch(file.ContentType, "image/\\S+") &&
file.ContentLength > 0)
{
DisplayImageInfo(file);
return true;
}
//Bestand is geen plaatje, dus hiervan een melding geven
AppendMessage(String.Format("U heeft geen bestand geselecteerd of er is een verkeerd bestandstype geupload. Alleen jpg, bmp en gif is toegestaan."));
return false;
}
protected void UploadImageWithNoRestrictions(HttpPostedFile file)
{
if(IsImage(file))
{
UploadImage(file);
}
}
protected Bitmap GetResizedImage(Bitmap bitmap)
{
//Ratio berekenen (breedte/hoogte)
ThumbnailRatio = Convert.ToDouble(bitmap.Width) / Convert.ToDouble(bitmap.Height);
//Plaatje is in verhouding een breed plaatje
if (ThumbnailRatio > 1.7)
{
ThumbnailWidth = ThumbnailSizeWidth;
ThumbnailHeight = ThumbnailSizeWidth / ThumbnailRatio;
}
//Plaatje is in verhouding redelijk vierkant
else if ((ThumbnailRatio < 1.7) && (ThumbnailRatio > 1))
{
ThumbnailWidth = ThumbnailSizeWidthSquare;
ThumbnailHeight = ThumbnailSizeWidthSquare / ThumbnailRatio;
}
//Plaatje is in verhouding hoog
else
{
ThumbnailHeight = ThumbnailSizeHigh;
ThumbnailWidth = ThumbnailSizeHigh / ThumbnailRatio;
}
return new Bitmap(bitmap, Convert.ToInt32(ThumbnailWidth), Convert.ToInt32(ThumbnailHeight));
}
protected void UploadImage(HttpPostedFile file)
{
//Werkgever ID meegeven in de bestandsnaam om overschrijving te voorkomen
EmpID = Request.QueryString["gid"];
//Opslaan van originele plaatje
string fileName =
file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1);
string filePath = MapPath("logos/" + EmpID + "_" + fileName);
file.SaveAs(filePath);
using(System.Drawing.Image image =
System.Drawing.Image.FromStream(file.InputStream))
//Opslaan van geresizede plaatje
using (Bitmap bitmap = GetResizedImage((Bitmap) image))
{
bitmap.Save(MapPath("logos/t__" + EmpID + "_" + fileName), image.RawFormat);
}
//Na het opslaan van het geresizede plaatje kan het originele plaatje verwijderd worden
File.Delete(MapPath("logos/" + EmpID + "_" + fileName));
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}