List Object’s Properties in Javascript

July 8, 2009

Here is the Code to List all the Properties of an Object in JavaScript

for (var propertyName in Object)
{
    document.writeln(propertyName);
    document.write(‘<BR>’)
}

Note:- Object will be replaced by your objectName 🙂


Resize Bitmap Image with Aspect Ratio

July 4, 2009

Just Copy this function into your code and use it to resize the bitmap image with corresponding aspect Ratio.

private Bitmap ResizeImage(Bitmap image, int width, int height)
{
Bitmap OriginalBmp = image;

int OriginalWidth = OriginalBmp.Width;
int OriginalHeight = OriginalBmp.Height;
int OriginalX = 0;
int OriginalY = 0;
int NewX = 0;
int NewY = 0;

float Percent = 0;
float WidthPercent = 0;
float HeightPercent = 0;

WidthPercent = ((float)width / (float)OriginalWidth);
HeightPercent = ((float)height / (float)OriginalHeight);
if (HeightPercent < WidthPercent)
{
Percent = HeightPercent;
NewX = System.Convert.ToInt32((width – (OriginalWidth * Percent)) / 2);
}
else
{
Percent = WidthPercent;
NewY = System.Convert.ToInt32((height – (OriginalHeight * Percent)) / 2);
}

int DestWidth = (int)(OriginalWidth * Percent);
int DestHeight = (int)(OriginalHeight * Percent);

Bitmap NewImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
NewImage.SetResolution(OriginalBmp.HorizontalResolution, OriginalBmp.VerticalResolution);

Graphics Gr = Graphics.FromImage(NewImage);
Gr.Clear(Color.White);
Gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

Gr.DrawImage(OriginalBmp, new Rectangle(NewX, NewY, DestWidth, DestHeight), new Rectangle(OriginalX, OriginalY, OriginalWidth, OriginalHeight), GraphicsUnit.Pixel);
Gr.Dispose();
OriginalBmp.Dispose();
MemoryStream Ms = new MemoryStream();
return NewImage;
}


Creating IN Queries With Linq To Sql

June 24, 2009

An IN query will pull back a set of results from SQL that is within a given range. This range can be set manually, or can itself be a query. So if you have an eCommerce application and you want to know what products you have in a given user’s cart, you could do this

SELECT * FROM Production.Product WHERE ProductID IN (SELECT ProductID FROM Sales.ShoppingCartItem WHERE ShoppingCartID=’RobsCart’)

you could do this in LINQ like this:

var itemQuery = from cartItems in db.SalesOrderDetails
                select cartItems.ProductID;


var myProducts = from p in db.Products
                where itemQuery.Contains(p.ProductID)
                select p;

Here is the key to this weirdness:

Linq To Sql only constructs the query when the Enumerator is tripped.