Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, February 27, 2013

How to make thumbnail of an image and fix this in center position of div using asp.net and c#

First you have to create a page of CreateThumbnail.aspx and paste this below code :-
    protected void Page_Load(object sender, EventArgs e)
    {
        string Image = Request.QueryString["Image"];

        if (Image == null)
        {
            this.ErrorResult();
            return;
        }

        string sSize = Request["Size"];
        int Size = 120;
        if (sSize != null)
            Size = Int32.Parse(sSize);

        string Path = Server.MapPath(Request.ApplicationPath) + "\\" + Image;
        Bitmap bmp = CreateThumbnail(Path, Size, Size);

        if (bmp == null)
        {
            this.ErrorResult();
            return;
        }

        string OutputFilename = null;
        OutputFilename = Request.QueryString["OutputFilename"];

        if (OutputFilename != null)
        {
            if (this.User.Identity.Name == "")
            {
                // *** Custom error display here
                bmp.Dispose();
                this.ErrorResult();
            }

            try
            {
                bmp.Save(OutputFilename);
            }

            catch (Exception ex)
            {
                bmp.Dispose();
                this.ErrorResult();
                return;
            }
        }

        // Put user code to initialize the page here
        Response.ContentType = "image/jpeg";
        bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        bmp.Dispose();
    }

    private void ErrorResult()
    {
        Response.Clear();
        Response.StatusCode = 404;
        Response.End();
    }

    public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
    {
        System.Drawing.Bitmap bmpOut = null;
        try
        {
            Bitmap loBMP = new Bitmap(lcFilename);
            ImageFormat loFormat = loBMP.RawFormat;

            decimal lnRatio;
            int lnNewWidth = 0;
            int lnNewHeight = 0;

            //*** If the image is smaller than a thumbnail just return it
            if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
                return loBMP;

            if (loBMP.Width > loBMP.Height)
            {
                lnRatio = (decimal)lnWidth / loBMP.Width;
                lnNewWidth = lnWidth;
                decimal lnTemp = loBMP.Height * lnRatio;
                lnNewHeight = (int)lnTemp;
            }
            else
            {
                lnRatio = (decimal)lnHeight / loBMP.Height;
                lnNewHeight = lnHeight;
                decimal lnTemp = loBMP.Width * lnRatio;
                lnNewWidth = (int)lnTemp;
            }

            // System.Drawing.Image imgOut = loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight, null,IntPtr.Zero);

            // *** This code creates cleaner (though bigger) thumbnails and properly
            // *** and handles GIF files better by generating a white background for
            // *** transparent images (as opposed to black)
            bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
            Graphics g = Graphics.FromImage(bmpOut);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
            g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

            loBMP.Dispose();
        }
        catch
        {
            return null;
        }
        return bmpOut;
    }
How to call this page on demo page :-
Add this script to set image in center position of div :-
      
      

Friday, November 23, 2012

FileUpload control is not working within update panel in asp.net

Hi All,

Today i face a problem with uploading a file using file upload control of asp.net. Because it was in the update panel. It's giving blank in fupImage.FileName. But after searched on web i found a solution on Here. 

It says that paste this code in your page load part :-

Page.Form.Attributes.Add("enctype", "multipart/form-data");
But this was not worked for me :( Finally after so many searches i found a simple solution for this. That i want to share with you :-
 
Solution :

You just need to make the your button to upload the file as the trigger of the Upload panel Something like this : -



 

Friday, October 12, 2012

What is the difference between String and string

string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32.

As far as guidelines, I think it's generally recommended to use string any time you're referring to an object. e.g.


string place = "world";

Likewise, I think it's generally recommended to use String if you need to refer
specifically the class. e.g.


string greet = String.Format("Hello {0}!", place);

Thursday, September 20, 2012

How to find distance between two locations, using latitude and longitude in asp.net with c#

I am posting here how to calculate distance between two location. For this you have to need Latitude and Longitude for both of locations. Than you have to just call my function as like :-

Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "M"));
Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "K"));
Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "N"));

Use 'M' for Mile, 'K' for kilometer in unit parameter.

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::  This function calculating the distace between 2 location      :::
//::  Author : Sandeep Kumar Tawaniya                               :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private double distance(double lat1, double lon1, double lat2, double lon2, char unit)
{
    double theta = lon1 - lon2;
    double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
    dist = Math.Acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    if (unit == 'K')
    {
        dist = dist * 1.609344;
    }
    else if (unit == 'N')
    {
        dist = dist * 0.8684;
    }
    return (dist);
}

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::  This function converts decimal degrees to radians             :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private double deg2rad(double deg)
{
    return (deg * Math.PI / 180.0);
}

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
//::  This function converts radians to decimal degrees             :::
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
private double rad2deg(double rad)
{
    return (rad / Math.PI * 180.0);
}

Tuesday, June 19, 2012

Difference between Class and Structure in .NET

1. Classes are reference types and structs are value types. Since classes are reference type, a class variable can be assigned null.But we cannot assign null to a struct variable, since structs are value type.

2. When you instantiate a class, it will be allocated on the heap.When you instantiate a struct, it gets created on the stack.

3. You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct ( but dealing directly with them).

4. When passing a class to a method, it is passed by reference. When passing a struct to a method, it’s passed by value instead of as a reference.

5. You cannot have instance Field initializers in structs. But classes can have
For eg:

class MyClass
{
   int myVar =10; // no syntax error.
   public void MyFun( )
   {
     // statements
   }
}

struct MyStruct
{
   int myVar = 10; // syntax error.
   public void MyFun( )
   {
     // statements
   }
}

6. Classes can have explicit parameterless constructors. But structs cannot have

7. Classes must be instantiated using the new operator. But structs can be

8. Classes support inheritance.But there is no inheritance for structs. (structs don’t support inheritance polymorphism)

9. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.

10. A class is permitted to declare a destructor. But a struct is not

12. classes are used for complex and large set data. structs are simple to use.

Friday, May 25, 2012

How to convert DataTable to XML in C#?

Following code illustrates about converting a DataTable to XML format. This is often required when passing a DataTable to a stored procedure. We can pass an XML directly to the procedure and process it.

private string ConvertDataTableToXML(DataTable dtBuildSQL)
{

  DataSet dsBuildSQL = new DataSet();

  StringBuilder sbSQL;
  StringWriter swSQL;
  string XMLformat;

  sbSQL = new StringBuilder();
  swSQL = new StringWriter(sbSQL);
  dsBuildSQL.Merge(dtBuildSQL, true, MissingSchemaAction.AddWithKey);
 dsBuildSQL.Tables[0].TableName = "Table";
 foreach (DataColumn col in dsBuildSQL.Tables[0].Columns)
 {
    col.ColumnMapping = MappingType.Attribute;
 }
 dsBuildSQL.WriteXml(swSQL);
 XMLformat = sbSQL.ToString();
 return XMLformat;
} 

Monday, December 12, 2011

How to send/receive SOAP request and response and return in dataset using C#

public Dataset GetCountries (string _agentCode, string _password)
       {
           WebRequest webRequest = WebRequest.Create("http://www.flexiblecarhire.com/flexibleservice/fchxmlinterface.asmx");
           HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
           httpRequest.Method = "POST";
           httpRequest.ContentType = "text/xml; charset=utf-8";
           httpRequest.Headers.Add("SOAPAction: http://fchhost.org/GetCountries");
           Stream requestStream = httpRequest.GetRequestStream();
           //Create Stream and Complete Request           
           StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);


           StringBuilder soapRequest = new StringBuilder("");
           soapRequest.Append("");
           soapRequest.Append("" + _agentCode + "");
           soapRequest.Append("" + _password + "");
           soapRequest.Append("");

           streamWriter.Write(soapRequest.ToString());
           streamWriter.Close();
           //Get the Response  
           HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
           StreamReader srd = new StreamReader(wr.GetResponseStream());
         
           DataSet ds = new DataSet();
           ds.ReadXml(wr.GetResponseStream());

           return ds;
       }

Friday, May 7, 2010

String Formats For Decimal Values

txtFabricProcessingCost.Text = String.Format("{0:F2}", totalProcessCost);

txtFabricProcessingCost.Text = totalProcessCost.ToString("#0.00");

txtFabricProcessingCost.Text = totalProcessCost.ToString("N2");
txtFabricProcessingCost.Text = totalProcessCost.ToString("N3");

double v = 17688.65849;
double v2 = 0.15;
int x = 21;
string str = String.Format("{0:F2}", v); // 17688.66

str = String.Format("{0:N5}", v); //17,688.65849

str = String.Format("{0:e}", v); //1.768866e+004

str = String.Format("{0:r}", v); //17688.65849

str = String.Format("{0:p}", v2); //15.00%

str = String.Format("{0:X}", x); //15

str = String.Format("{0:D12}", x); //000000000021

str = String.Format("{0:C}", 189.99); //$189.99