Mini Shell

Direktori : C:/Inetpub/vhosts/protarte.com/httpdocs/admin/
Upload File :
Current File : C:/Inetpub/vhosts/protarte.com/httpdocs/admin/inserir.aspx.cs

using System;
using System.Threading;
using System.Web.UI;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Drawing.Imaging;

public partial class admin_inserir : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["protarteConnectionString"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        if (TextBox3.Text!= "")
        {

            VaryQualityLevel();
            VaryQualityLevel2();

            string sql;

            string cod = "<li class=''item-thumbs span3 cabeca''> <span style=''font-size:25px; display:block; text-align:center; width:100%; margin-bottom:10px''>" + TextBox1.Text + "</span>  <a class=''hover-wrap fancybox'' data-fancybox-group=''gallery'' title=''" + TextBox1.Text + "'' target=''_blank'' href=''_include/img/work/ficha/" + FileUpload2.FileName + "''> <span class=''overlay-img''></span> <span class=''overlay-img-thumb font-icon-plus''></span> </a><img src=''_include/img/work/thumbs/" + FileUpload1.FileName + "'' > </li>";

            sql = "INSERT INTO produto(imagem , tit, cat, cod, url) VALUES ('"+ FileUpload1.FileName +"', '"+ TextBox1.Text +"' , '"+ dp.SelectedValue.ToString() +"', '"+ cod +"' , '" +FileUpload2.FileName+"' ) SELECT @@IDENTITY";



            SqlCommand insert3 = new SqlCommand(sql, con);


            try
            {
                con.Open();
                int n = insert3.ExecuteNonQuery();
                con.Close();

                if (n != 0)
                {

                    TextBox1.Text = "";
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Artigo inserido com sucesso!');", true);



                }



            }
            catch (Exception ex)
            {
                TextBox1.Text = ex.ToString();
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex.Message + "');", true);
            }
            finally
            {
                con.Close();

            }
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Por favor selecionar categoria');", true);
        }
    }
        


    //funcao para crop tamnho imagem-----------------------------------------------------------
    public System.Drawing.Bitmap ProportionallyResizeBitmap(System.Drawing.Bitmap src, int maxWidth, int maxHeight)
    {

        // original dimensions
        int w = src.Width;
        int h = src.Height;

        // Longest and shortest dimension
        int longestDimension = (w > h) ? w : h;
        int shortestDimension = (w < h) ? w : h;

        // propotionality
        float factor = ((float)longestDimension) / shortestDimension;

        // default width is greater than height
        double newWidth = maxWidth;
        double newHeight = maxWidth / factor;

        // if height greater than width recalculate
        if (w < h)
        {
            newWidth = maxHeight / factor;
            newHeight = maxHeight;
        }

        // Create new Bitmap at new dimensions
        System.Drawing.Bitmap result = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)result))
            g.DrawImage(src, 0, 0, (int)newWidth, (int)newHeight);

        return result;


    }

    //funcao para crop tamnho imagem-----------------------------------------------------------

    //funcao para uploadfotos comentarios-------------------------------------------------------------------------
    private void VaryQualityLevel()
    {
        try
        {

            HttpPostedFile pf = FileUpload1.PostedFile;
            System.Drawing.Image bm = System.Drawing.Image.FromStream(pf.InputStream);
            bm = ProportionallyResizeBitmap((System.Drawing.Bitmap)bm, 570, 400); /// new width, height


            // Get a bitmap.
            System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(bm);
            ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;

            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
            // EncoderParameter object in the array.
            EncoderParameters myEncoderParameters = new EncoderParameters(1);

            EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            bmp1.Save(Path.Combine(Server.MapPath("../_include/img/work/thumbs/"), pf.FileName), jgpEncoder, myEncoderParameters);

            myEncoderParameter = new EncoderParameter(myEncoder, 80L);
            myEncoderParameters.Param[0] = myEncoderParameter;
            bmp1.Save(Path.Combine(Server.MapPath("../_include/img/work/thumbs/"), pf.FileName), jgpEncoder, myEncoderParameters);

        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ex.Message + "');", true);
        }



    }
    //funcao para uploadfotos comentarios-------------------------------------------------------------------------




    //funcao para uploadfotos publicaçoes------------------------------------------------------------------------
    private void VaryQualityLevel2()
    {
       

         if( ( FileUpload2.PostedFile != null ) && ( FileUpload2.PostedFile.ContentLength > 0 ) )
	    {
		string fn = System.IO.Path.GetFileName(FileUpload2.PostedFile.FileName);
		string SaveLocation = Server.MapPath("../_include/img/work/ficha/"+ fn);
		try
		{
			FileUpload2.PostedFile.SaveAs(SaveLocation);
			
		}
		catch ( Exception ex )
		{
			Response.Write("Error: " + ex.Message);
			//Note: Exception.Message returns detailed message that describes the current exception. 
			//For security reasons, we do not recommend you return Exception.Message to end users in 
			//production environments. It would be better just to put a generic error message. 
		}
	
		}
	

	else
	{
		Response.Write("Por favor selecionar ficha tecnica");
        return;
	}



    }


    private ImageCodecInfo GetEncoder(ImageFormat format)
    {

        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

        foreach (ImageCodecInfo codec in codecs)
        {
            if (codec.FormatID == format.Guid)
            {
                return codec;
            }
        }
        return null;
    }
    protected void dp_SelectedIndexChanged(object sender, EventArgs e)
    {
      TextBox3.Text =  dp.SelectedValue.ToString();
    }
}

Zerion Mini Shell 1.0