Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2007, 04:42 AM
Registered User
 
Join Date: Jan 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default uploading/resizing (transparant) images in C#

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
    }
}
 
Old February 19th, 2007, 08:53 PM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi. I am doing something similar to that. I am looking for a solution but I don't find anything yet. I'm not sure if you have found a way to do this.
I hope you can help me.
Regards






Similar Threads
Thread Thread Starter Forum Replies Last Post
picture resizing before uploading mark jonas Beginning PHP 1 July 4th, 2007 08:04 AM
Resizing Images san123 ASP.NET 1.0 and 1.1 Basics 2 April 26th, 2006 02:51 PM
Resizing images according to paper size Taxidriver Crystal Reports 3 March 28th, 2006 06:08 AM
GDI+ - Issue Resizing images Hubman VS.NET 2002/2003 2 April 16th, 2004 08:11 PM
Resizing Images From a Database mayhew3 Pro PHP 1 March 3rd, 2004 07:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.