Saturday, February 5, 2011

Save Image to Disk using C#.Net

After lot of search I found the code that saves Image class to harddisk.

public void SaveImage(Image imgToSave, string FileName)
        {
            //Create empty bitmap image of original size

            Bitmap tempBmp = new Bitmap(imgToSave.Width, imgToSave.Height);

            Graphics g = Graphics.FromImage(tempBmp);
            //draw the original image on tempBmp

            g.DrawImage(imgToSave, 0, 0, imgToSave.Width, imgToSave.Height);


            //dispose originalImage and Graphics so the file is now free

            g.Dispose();

            imgToSave.Dispose();

            //Save the image file to original location

            tempBmp.Save(FileName, System.Drawing.Imaging.ImageFormat.Jpeg);

            
        }

Friday, February 4, 2011

Code For Fetch Images from a URL using C#.net

public List FetchImages(string Url)
        {
            List imageList = new List();

            //Append http:// if necessary
            if (!Url.StartsWith("http://") && !Url.StartsWith("https://"))
                Url = "http://" + Url;

            string responseUrl = string.Empty;
            string htmlData = ASCIIEncoding.ASCII.GetString(DownloadData(Url, out responseUrl));

            if (responseUrl != string.Empty)
                Url = responseUrl;

            if (htmlData != string.Empty)
            {
                string imageHtmlCode = "'); //make sure data will be inside img tag
                    int start = htmlData.IndexOf(imageSrcCode) + imageSrcCode.Length;
                    int end = htmlData.IndexOf('"', start + 1);

                    //Extract the line
                    if (end > start && start < brackedEnd)
                    {
                        string loc = htmlData.Substring(start, end - start);

                        //Store line
                        imageList.Add(loc);
                    }

                    //Move index to next image location
                    if (imageHtmlCode.Length < htmlData.Length)
                        index = htmlData.IndexOf(imageHtmlCode, imageHtmlCode.Length);
                    else
                        index = -1;
                }

                //Format the image URLs
                for (int i = 0; i < imageList.Count; i++)
                {
                    string img = imageList[i];

                    string baseUrl = GetBaseURL(Url);

                    if ((!img.StartsWith("http://") && !img.StartsWith("https://"))
                        && baseUrl != string.Empty)
                        img = baseUrl + "/" + img.TrimStart('/');

                    imageList[i] = img;
                }
            }

            return imageList;
        }

   
        private byte[] DownloadData(string Url)
        {
            string empty = string.Empty;
            return DownloadData(Url, out empty);
        }

        private byte[] DownloadData(string Url, out string responseUrl)
        {
            byte[] downloadedData = new byte[0];
            try
            {
                //Get a data stream from the url
                WebRequest req = WebRequest.Create(Url);
                WebResponse response = req.GetResponse();
                Stream stream = response.GetResponseStream();

                responseUrl = response.ResponseUri.ToString();

                //Download in chuncks
                byte[] buffer = new byte[1024];

                //Get Total Size
                int dataLength = (int)response.ContentLength;

                //Download to memory
                //Note: adjust the streams here to download directly to the hard drive
                MemoryStream memStream = new MemoryStream();
                while (true)
                {
                    //Try to read the data
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);

                    if (bytesRead == 0)
                    {
                        break;
                    }
                    else
                    {
                        //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);
                    }
                }

                //Convert the downloaded stream to a byte array
                downloadedData = memStream.ToArray();

                //Clean up
                stream.Close();
                memStream.Close();
            }
            catch (Exception)
            {
                responseUrl = string.Empty;
                return new byte[0];
            }

            return downloadedData;
        }

Code for Image to Byte Array and Byte Array to Image Converter using C#

Convert Image to byte[] array
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return  ms.ToArray();
}
Convert byte[] array to Image
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

Get File Name from URL (C#/.NET)

string FileName = URL.Substring(URL.LastIndexOf("/") + 1,
(URL.Length - URL.LastIndexOf("/") - 1));

Or:

string fileName = Path.GetFileName(url);

Ref:

http://www.thejackol.com/2007/04/10/get-file-name-from-url-cnet/

Tuesday, January 25, 2011

Resolved : Debugging Timeout on IIS 7 And Visual Studio

In IIS 7 go to the Application Pools and select the Advanced Settings for the pool which you process runs in. In the Process Model section you can do one of two things;

* Set the Ping Enabled property to False. This will stop IIS checking to see if the worker process is still running and keep your process alive permanently (or until you stop your debugged process)
* If you prefer to allow IIS to continue the monitoring process then change the Ping Maximum Response Timeout value to something larger than 90 seconds (default value).

Wednesday, January 12, 2011

Sticky Footer Or Footer At bottom of the web page

Check the following articles that explain the css hack to keep footer at bottom of the page.
http://ryanfait.com/resources/footer-stick-to-bottom-of-page/

http://ryanfait.com/sticky-footer/
http://www.cssstickyfooter.com/
http://www.cssstickyfooter.com/using-sticky-footer-code.html

In case, above links are not working, check below.
CSS::

* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -4em;
}
.footer, .push {
height: 4em;
}



<html>
<head>
<link rel="stylesheet" href="layout.css" ... />
</head>
<body>
<div class="wrapper">
<p>Your website content here.</p>
<div class="push"></div>
</div>
<div class="footer">
<p>Copyright (c) 2008</p>
</div>
</body>
</html>

Multicolumn Layout With Sticky Footer

Add clear to the .push div
.footer, .push {
clear: both;
}

Monday, December 27, 2010

Mozilla Addons

Firebug

https://addons.mozilla.org/en-US/firefox/addon/1843/

CopyAllURLs
https://addons.mozilla.org/en-US/firefox/addon/934/

ColorZilla

https://addons.mozilla.org/en-US/firefox/addon/271/

FireShot 0.87


https://addons.mozilla.org/en-US/firefox/addon/5648/

Find All Addons
https://addons.mozilla.org/en-US/firefox/