Archive

Archive for February, 2009

Add Text to Upload Image

February 28, 2009 Leave a comment

Hi

try this example to Add Text to Uploaded Image and save it in folder in my website

Step1 :- create a website and add Upload folder and web page

Step2 :- in web page add fileupload and Button control

and in code behind :

protected void Button1_Click(object sender, EventArgs e)   
    {       
        System.Drawing.Image i = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);       
        Bitmap b = yaz(i, i.Width, i.Height, "Yasser Zaid", 16);        
        b.Save(Server.MapPath("~/Upload/" + FileUpload1.FileName));    
    }

    private Bitmap yaz(System.Drawing.Image resim, int genislik, int yukseklik, string yazilacak, float font)   
    {       
        Bitmap resmim = new Bitmap(resim, genislik, yukseklik);       
        System.Drawing.Graphics graf = System.Drawing.Graphics.FromImage(resmim);       
        System.Drawing.SolidBrush firca = new SolidBrush(System.Drawing.Color.Red);       
        System.Drawing.Font fnt = new Font("calibri", font);  
        System.Drawing.SizeF size = new SizeF(0, 0);       
        System.Drawing.PointF coor = new PointF(0,0);       
        System.Drawing.RectangleF kutu = new RectangleF(coor, size);        
        StringFormat sf = new StringFormat();       
        sf.FormatFlags = StringFormatFlags.DirectionVertical;        
        graf.DrawString(yazilacak, fnt, firca, kutu, sf);       
        return resmim;   
    }

Hope this helsp

Good Luck

Advertisements
Categories: ASP.Net

Generate Curved Image

February 28, 2009 Leave a comment

Hi

try this example:-

Add new web page and add FileUpload and Button control

and in code behind:-

protected void Button1_Click(object sender, EventArgs e)
    {
        //string path = Server.MapPath("~/Images/602869611.jpg");
        string path = FileUpload1.PostedFile.FileName;

        int roundedDia = 50;
        using (System.Drawing.Image imgin = System.Drawing.Image.FromFile(path))
        {
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imgin.Width, imgin.Height);
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);
            Brush brush = new System.Drawing.TextureBrush(imgin);
            FillRoundedRectangle(g, new Rectangle(0, 0, imgin.Width, imgin.Height), roundedDia, brush);
            // done with drawing dispose graphics object.
            g.Dispose();
            // Stream Image to client.
            Response.Clear();
            Response.ContentType = "image/pjpeg";
           // bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            Response.End();
            // dispose bitmap object.
            bitmap.Dispose();
        
        }
    }

    public static void FillRoundedRectangle(Graphics g, Rectangle r, int d, Brush b)
    {
        // anti alias distorts fill so remove it.
        System.Drawing.Drawing2D.SmoothingMode mode = g.SmoothingMode;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
        g.FillPie(b, r.X, r.Y, d, d, 180, 90);
        g.FillPie(b, r.X + r.Width - d, r.Y, d, d, 270, 90);
        g.FillPie(b, r.X, r.Y + r.Height - d, d, d, 90, 90);
        g.FillPie(b, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90);
        g.FillRectangle(b, r.X + d / 2, r.Y, r.Width - d, d / 2);
        g.FillRectangle(b, r.X, r.Y + d / 2, r.Width, r.Height - d);
        g.FillRectangle(b, r.X + d / 2, r.Y + r.Height - d / 2, r.Width - d, d / 2);
        g.SmoothingMode = mode;
    }

Hope this helps

Good Luck

Categories: ASP.Net

Get Uploaded Image Information

February 28, 2009 5 comments

Hi

try this example:-

//-- Get Image Hieght and width
        System.Drawing.Image objImage = System.Drawing.Image.FromFile(FileUpload1.PostedFile.FileName);
        int width = objImage.Width;
        int height = objImage.Height;

        //-- Get Image Size
        string size=FileUpload1.PostedFile.ContentLength.ToString();

        //-- Get Image Extension
        string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
        string extension2=FileUpload1.PostedFile.ContentType;

Hope this helps

Good Luck

Categories: ASP.Net

Resize Uploaded Image

February 28, 2009 2 comments

Hi

try this example to Resize Uploaded Image Hieght and Width and upload  it in folder

Step1:- create a website and add web page and Upload folder where i will upload image on it

step2 :- in web page

<div>
        <strong>Resize Image Hieght and Width</strong><br />
        <br />
        <asp:FileUpload ID="MyFile" runat="server" /><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload Image" />&nbsp;<br />
        <br />
    </div>
        <div id="FileDetails" runat="server" visible="false">FileName: <span id="FileName" runat="server"></span><br/>
        ContentType: <span id="MyContentType" runat="server"></span><br/>
        ContentLength: <span id="ContentLength" runat="server">bytes
            </span><br/><br/>
        Original Image : <asp:Image id="Image1" runat="server"></asp:Image><br/>
  Resized  Image : <asp:Image id="Image2" runat="server"></asp:Image>
        </div>

and in code behind :-

protected void Button1_Click(object sender, EventArgs e)
    {
        String UploadedFile = MyFile.PostedFile.FileName;
        int ExtractPos = UploadedFile.LastIndexOf("\\") + 1;
        //to retrieve only Filename from the complete path
        String UploadedFileName = UploadedFile.Substring
        (ExtractPos, UploadedFile.Length - ExtractPos);

        // Display information about posted file. Div is invisible by default  
        FileName.InnerHtml = UploadedFileName;
        MyContentType.InnerHtml = MyFile.PostedFile.ContentType;
        ContentLength.InnerHtml= MyFile.PostedFile.ContentLength.ToString();

        FileDetails.Visible = true; //div is made visible
        // Save uploaded file to server at the in the Pics folder
        MyFile.PostedFile.SaveAs(Request.PhysicalApplicationPath + "Upload\\" + UploadedFileName);
        //thumbnail creation starts

        try
        {
            //Retrieve the image filename whose thumbnail has to be created
            String imageUrl = UploadedFileName;
            //Read in the width and height
            int imageHeight = 20; //-- new image Hieght
            int imageWidth = 20;  //-- new image width
            if (imageUrl.IndexOf("/") >= 0 || imageUrl.IndexOf("\\") >= 0)
            {
                //We found a / or \
                Response.End();
            }

            //the uploaded image will be stored in the Pics folder.
            //to get resize the image, the original image has to be
            //accessed from the Pics folder
            imageUrl = "Upload/" + imageUrl;
            System.Drawing.Image fullSizeImg= System.Drawing.Image.FromFile(Server.MapPath(imageUrl));
            System.Drawing.Image.GetThumbnailImageAbort dummyCallBack= new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            System.Drawing.Image thumbNailImg= fullSizeImg.GetThumbnailImage(imageWidth, imageHeight,dummyCallBack, IntPtr.Zero);

            //We need to create a unique filename for each generated image
            DateTime MyDate = DateTime.Now;

            String MyString = MyDate.ToString("ddMMyyhhmmss") + ".png";
            //Save the thumbnail in PNG format.
            //You may change it to a diff format with the ImageFormat property
            thumbNailImg.Save(Request.PhysicalApplicationPath+ "Upload\\" + MyString, ImageFormat.Png);
            thumbNailImg.Dispose();

            //Display the original & the newly generated thumbnail

            Image1.AlternateText = "Original image";
            Image1.ImageUrl = "Upload\\" + UploadedFileName;
            Image2.AlternateText = "Thumbnail";
            Image2.ImageUrl = "Upload\\" + MyString;

        }
        catch (Exception ex)
        {
            Response.Write("An error occurred - " + ex.ToString());
        }
    }

    //this function is reqd for thumbnail creation
    public bool ThumbnailCallback()
    {
        return false;
    }

–Use these namespace

using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.IO;

Hope this helps

Good Luck

Categories: ASP.Net

Generate Thumbnail Image

February 28, 2009 Leave a comment

Hi

try this example to Generate Thumbnail Image of Uploaded image

step1 :- create a website and add web page and image folder and inside it add thumbs folder where in image folder

we will upload image and in thumbs folder we will upload thumbnail image

step2 : in web page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">

<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Uplad Et" /><br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label><br />
        <br />
        <asp:Image ID="Image1" runat="server" />
  </div>
    </form>
</body>
</html>

and in code behind :-

public void upload()
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/images/") + FileUpload1.FileName);
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/images/") + FileUpload1.FileName);
            System.Drawing.Image thumb = img.GetThumbnailImage(160, 120, null, IntPtr.Zero);
            img.Dispose();
            thumb.Save(Server.MapPath("~/images/thumbs/") + FileUpload1.FileName);
            //
            Label1.Text = "Upload edilen dosya : " + FileUpload1.FileName + "<br/><br/>Olu‏turulan küçük resim";
            Image1.ImageUrl = "~/images/thumbs/" + FileUpload1.FileName;
        }
    }

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

Hope this helps

Good Luck

Categories: ASP.Net Tags:

Change Color of Uploaded Image to Black and White

February 28, 2009 Leave a comment

Hi

try this example to change Color of Uploaded Image to Black and White:-

step1 :- create website and add web page and Upload folder and inside Upload folder add Original folder

Step2 :- in web page add the following code

<div>
    <p>Please upload your picture:
    <asp:FileUpload id="PictureUploadControl" Width="400" runat="server" />       
    </p>
        <p>
    <asp:Button runat="server" id="AddPictureButton" text="Upload" onclick="AddPictureButton_Click" /><br />
            &nbsp;</p>
   
    <asp:Image ID="GrayscaledPhoto" runat="server" /><br />

</div>

in code behind :-

public static Bitmap Grayscale(Bitmap bitmap)
    {
        //Declare myBitmap as a new Bitmap with the same Width & Height
        Bitmap myBitmap = new Bitmap(bitmap.Width, bitmap.Height);

        for (int i = 0; i < bitmap.Width; i++)
        {
            for (int x = 0; x < bitmap.Height; x++)
            {
                //Get the Pixel
                Color BitmapColor = bitmap.GetPixel(i, x);
                //I want to come back here at some point and understand, then change, the constants
                //Declare grayScale as the Grayscale Pixel
                int grayScale = (int)((BitmapColor.R * 0.3) + (BitmapColor.G * 0.59) + (BitmapColor.B * 0.11));
                //Declare myColor as a Grayscale Color
                Color myColor = Color.FromArgb(grayScale, grayScale, grayScale);
                //Set the Grayscale Pixel
                myBitmap.SetPixel(i, x, myColor);
            }
        }
        return myBitmap;
    }

    protected void AddPictureButton_Click(object sender, EventArgs e)
    {
        if (PictureUploadControl.HasFile)
        {
            PictureUploadControl.SaveAs(Server.MapPath("~/Upload/Original") + PictureUploadControl.FileName);
            Bitmap oldBitmap = new Bitmap(Server.MapPath("~/Upload/Original") + PictureUploadControl.FileName,

false);
            Bitmap newBitmap = Grayscale(new Bitmap(oldBitmap));
            string name = "grayscale";
            newBitmap.Save(Server.MapPath("~/Upload/") + name + ".jpg", ImageFormat.Jpeg);
            oldBitmap.Dispose();
            //we will delete the old
            File.Delete(Server.MapPath("~/Upload/Original") + PictureUploadControl.FileName);
            GrayscaledPhoto.ImageUrl = "Upload/" + name + ".jpg";
        }
    }

use these namespace

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

Good Luck

Categories: ASP.Net

Validate DropdwonList with RequiredField Validator

February 28, 2009 1 comment

Hi

try this example to validate bindable dropdownlist to make user must select item from it using RequiredField Validator:

<asp:DropDownList ID=”ddl_Area” runat=”server” AppendDataBoundItems=”True” DataSourceID=”SqlDataSource_Areas”
DataTextField=”AreaName” DataValueField=”AreaID” Width=”154px” >
<asp:ListItem Value=”-1″ >Select Area …</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID=”RequiredFieldValidator2″ runat=”server” ControlToValidate=”ddl_Area”
ErrorMessage=”Select Area.” InitialValue=”-1″  >*</asp:RequiredFieldValidator>

Hope it works

Good Luck

Categories: ASP.Net