Saturday, March 24, 2012

Uploading Image or file on FTP programatically using Asp.net File Uploader

Dear friends following is the programmatic solution which can used to upload any image on FTP using FTP credentials....

 public void ftpfile(string ftpfilepath)
 {
     string ftphost = "Domain";
     //here correct hostname or IP of the ftp server to be given  

     string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
     FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
     ftp.Credentials = new NetworkCredential("Domain", "Password");
     //userid and password for the ftp server to given  

     ftp.KeepAlive = true;
     ftp.UseBinary = true;
     ftp.Method = WebRequestMethods.Ftp.UploadFile;
     
     // Resize Image is function to resize the image 
     System.Drawing.Image img1 = 
         ResizeImage(FileUpload1.PostedFile.InputStream, 84, 118);

     // Convert the image into bytes Array;
     byte[] buffer = ImageToByte(img1);
 
     // Send the image file in form of bytes
     Stream ftpstream = ftp.GetRequestStream();
     ftpstream.Write(buffer, 0, buffer.Length);
     ftpstream.Close();
}

Hope this will work for you.....

Thursday, March 22, 2012

How to find blocking in SQL server and killing blocking process

Following command will give you detail of all the processes
sp_who2

You can find out all the process which have created blocking through following command
select cmd,* from sys.sysprocesses where blocked > 0

 You can kill the blocking process with the following command
kill 54

Thursday, March 8, 2012

How to Upload Image in ASP.net

Following code can be use to upload employee image and saving at server....
protected void btn_upload_Click(object sender, EventArgs e)
    {
        imageflag = 1;

        if (!FileUpload_image.HasFile)
        {
            Alert.Show("No file for uploading");
            return;
        }
        if (FileUpload_image.HasFile)
        {
            //string imagetype = FileUpload_image.PostedFile.ContentType;
            string[] acceptedTypes = new string[] 
            { 
                "image/bmp", 
                "image/jpeg", 
                "image/tiff", 
                "image/gif", 
                "image/png" 
                     };

            if (!acceptedTypes.Contains(FileUpload_image.PostedFile.ContentType))
            {
                Alert.Show("This is not image file");
                return;
            }
            Bitmap image = ResizeImage(FileUpload_image.PostedFile.InputStream, 84, 118);
            string imagename = Guid.NewGuid().ToString();
            image.Save(Server.MapPath("~/Employee Pictures/") + imagename + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            Img_Employee.ImageUrl = "~/Employee Pictures/" + imagename + ".jpg";
            image_path = "~/Employee Pictures/" + imagename + ".jpg";
        }


    }
    private Bitmap ResizeImage(Stream streamImage, int maxWidth, int maxHeight)
    {
        Bitmap originalImage = new Bitmap(streamImage);
        int newWidth = originalImage.Width;
        int newHeight = originalImage.Height;
        double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;

        if (aspectRatio <= 1 && originalImage.Width > maxWidth)
        {
            newWidth = maxWidth;
            newHeight = (int)Math.Round(newWidth / aspectRatio);
        }
        else if (aspectRatio > 1 && originalImage.Height > maxHeight)
        {
            newHeight = maxHeight;
            newWidth = (int)Math.Round(newHeight * aspectRatio);
        }

        return new Bitmap(originalImage, newWidth, newHeight);
    }

Drawing image using e.Graphics into Paint event of control

Following code will paint the image on Picture box paint Event....
 private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            // Create image.
            Image newImage = pictureBox1.Image;

            // Create coordinates for upper-left corner of image.
            int x = 100;
            int y = 100;

            // Create rectangle for source image.
            Rectangle srcRect = new Rectangle(facex, facey, 150, 150);
            GraphicsUnit units = GraphicsUnit.Pixel;

            // Draw image to screen.
            if(newImage != null)
            e.Graphics.DrawImage(newImage, x, y, srcRect, units);
            
        }

How to crop image in c#

Give the Image and location x,y with width and height and get the croped image

Rectangle srcRect = new Rectangle(LocX, Locy, width, heigth);

 private static Image cropImage(Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

How to convert image into byte array

Following is the function which will return bytes array after converting from image....

 public byte[] imageToByteArray(System.Drawing.Image imageIn)
 {
     MemoryStream ms = new MemoryStream();
     imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
     return ms.ToArray();
 }

How to Resize Image programaticaly in C#

Following is the function to resize the image, give the image, width and height to this function it will return the resized image....
private static Image resizeImage(Image imgToResize, int x, int y)
        {
            int destWidth = x;
            int destHeight = y;

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

Wednesday, March 7, 2012

How to set time out for service request in WCF

Time adjustments can categorizes in two scenarios.
1. Receiving Time settings at Application Side
2. Sending Time settings from Application

 To set data receiving timing at application we will have to make changes in the file named ServiceReferences.ClientConfig located at the root of the client application.

First Locate the binding of the service you want to increase timing of , under the basicHttpBinding tag. Now add the following code in the Binding Tag

openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"

 Now this will increase the time upto 10 mints for receiving and sending requests.

 your code will look some thing like that

<binding name="YourServiceName" closeTimeout="00:10:00"
 openTimeout="00:10:00" receiveTimeout="00:10:00" 
 sendTimeout="00:10:00"
 maxBufferSize="2147483647" 
 maxReceivedMessageSize="2147483647">
<security mode="None" />
Now to set that timing for server one should have to make changes to the servers Web.config File located at the root of the server application.

<bindings>
  <basicHttpBinding>
    <binding name=""  
  openTimeout="00:10:00" 
  receiveTimeout="00:10:00" 
  sendTimeout="00:10:00"
     </binding>
   </basicHttpBinding>
 </bindings>

This code will set the send and receive timing for all services to 10 min.

Tuesday, March 6, 2012

How to Trace WCF Exception

To trace a WCF Exception add the following code in the Web.config file under the configuration Tag
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning">
<listeners>
<add name="WcfLogSample" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="WcfLogSample" type="System.Diagnostics.XmlWriterTraceListener"
initializeData="D:\TracingFile.svclog" traceOutputOptions="Callstack" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>

This code will make trace file at the address in bold. open that file it will contain any exception occurred in your application