Difference between <% %>, <%= %>, <%# %>?

Whenever we are going to write code inside the <% %> then that piece of code will treated as server side code generally vbscript.

When we are accessing any server side variable into the HTML (in .aspx) side then we use <%= %>.

When we are using any binding control like repeater, gridview… etc then to bind the particular column with the database value then we are using <%# %> when the Autogenerate column of the binding controls are FALSE

For Example:

Text='<%#Eval("FirstName") %>' this tag put inside the Text property of the control inside the datagrid like (textbox, label etc....)

...S.VinothkumaR.

Image Resizing by WPF: Using System.Windows.Media.Imaging;

Here is the example of getting resized image by using Imaging namespace in DotNet 3.0.

Same three reference need to add for resizing the image also. PresentationCore, WindowsBase and PresentationFramework.


private void ResizeImage(string SourceImagePath, string ImageType, string TargetImagePath, int decodePixelWidth, int decodePixelHeight)

{

newWidth = 0;

newHeight = 0;

getResizableHeightWidth(SourceImagePath, decodePixelWidth, decodePixelHeight);

byte[] imageBytes = LoadImageData(SourceImagePath);

ImageSource imageSource = CreateResizedImage(imageBytes, newWidth, newHeight);

imageBytes = GetEncodedImageData(imageSource, ImageType);

SaveImageData(imageBytes, TargetImagePath);

}

LoadImageData, GetEncodedImageData and SaveImageData methods are given in last post. Here the CreateResizedImage method is given in below.


//Method for getting resized image by given image data byte array with width and height.

ImageSource CreateResizedImage(byte[] imageData, int width, int height)

{

BitmapImage bmpImage = new BitmapImage();

bmpImage.BeginInit();

if (width > 0)

{

bmpImage.DecodePixelWidth = width;

}

if (height > 0)

{

bmpImage.DecodePixelHeight = height;

}

bmpImage.StreamSource = new MemoryStream(imageData);

bmpImage.CreateOptions = BitmapCreateOptions.None;

bmpImage.CacheOption = BitmapCacheOption.OnLoad;

bmpImage.EndInit();

Rect rect = new Rect(0, 0, width, height);

DrawingVisual drawingVisual = new DrawingVisual();

using (DrawingContext drawingContext = drawingVisual.RenderOpen())

{

drawingContext.DrawImage(bmpImage, rect);

}

RenderTargetBitmap resizedImage = new RenderTargetBitmap(

(int)rect.Width, (int)rect.Height, // Resized dimensions

96, 96, // Default DPI values

PixelFormats.Default); // Default pixel format

resizedImage.Render(drawingVisual);

return resizedImage;

}


//Method for getting resizable height and width by given height and width.

private void getResizableHeightWidth(string ImagePath, int maxWidth, int maxHeight)

{

System.Drawing.Image image = new Bitmap(ImagePath);

int width = image.Width;

int height = image.Height;

double widthFactor = (Convert.ToDouble(width) / Convert.ToDouble(maxWidth));

double heightFactor = (Convert.ToDouble(height) / Convert.ToDouble(maxHeight));

newWidth=0;

newHeight=0;

if (widthFactor > heightFactor)

{

newWidth = Convert.ToInt32(Convert.ToDouble(width) / widthFactor);

newHeight = Convert.ToInt32(Convert.ToDouble(height) / widthFactor);

}

else

{

newWidth = Convert.ToInt32(Convert.ToDouble(width) / heightFactor);

newHeight = Convert.ToInt32(Convert.ToDouble(height) / heightFactor);

}

if (newHeight == 0)

{

newHeight = 1;

}

if (newWidth == 0)

{

newWidth = 1;

}

}



That’s it.


...S.VinothkumaR.

Image Cropping by WPF: Using System.Windows.Media.Imaging;

Introducing System.Windows.Media.Imaging namespace provides types that are used to encode and decode bitmap images.


Here I am going to explain a sample application which is used to cropping images. Imaging namespace is in under Windows Presentation Foundation will do the image manipulation in simple manner.


First of all, we need to add the following reference in our project.

- PresentationCore

- PresentationFramework

- WindowsBase


Then use the following namespace,


using System.Windows.Media;

using System.Windows.Media.Imaging;


The method for creating crop images with passing appropriate parameters as follows,


//Method for Creating Crop Image

private void CreateCropImage(int x, int y, int width, int height, string SourceImagePath, string TargetImagePath, string ImageType)

{

byte[] imgBytest = LoadImageData(SourceImagePath);

System.Drawing.Image img=new Bitmap(SourceImagePath);

CroppedBitmap cb = new CroppedBitmap((BitmapSource)CreateImage(imgBytest,img.Width,img.Height), new Int32Rect(x, y, width, height));

ImageSource imageSource = cb;

byte[] imageBytes = GetEncodedImageData(imageSource, ImageType);

SaveImageData(imageBytes, TargetImagePath);

}


//Method for getting image bytes from image's source path by using FileStream.

private static byte[] LoadImageData(string filePath)

{

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

BinaryReader br = new BinaryReader(fs);

byte[] imageBytes = br.ReadBytes((int)fs.Length);

br.Close();

fs.Close();

return imageBytes;

}

//Method for getting cropped image.

private static ImageSource CreateImage(byte[] imageData, int decodePixelWidth, int decodePixelHeight)

{

if (imageData == null) return null;

BitmapImage result = new BitmapImage();

result.BeginInit();

if (decodePixelWidth > 0)

{

result.DecodePixelWidth = decodePixelWidth;

}

if (decodePixelHeight > 0)

{

result.DecodePixelHeight = decodePixelHeight;

}

result.StreamSource = new MemoryStream(imageData);

result.CreateOptions = BitmapCreateOptions.None;

result.CacheOption = BitmapCacheOption.OnLoad;

result.EndInit();

return result;

}


//Method for getting encoded image data for the cropped image.

internal byte[] GetEncodedImageData(ImageSource image, string preferredFormat)

{

byte[] returnData = null;

BitmapEncoder encoder = null;

switch (preferredFormat.ToLower())

{

case ".jpg":

case ".jpeg":

encoder = new JpegBitmapEncoder();

break;

case ".bmp":

encoder = new BmpBitmapEncoder();

break;

case ".png":

encoder = new PngBitmapEncoder();

break;

case ".tif":

case ".tiff":

encoder = new TiffBitmapEncoder();

break;

case ".gif":

encoder = new GifBitmapEncoder();

break;

case ".wmp":

encoder = new WmpBitmapEncoder();

break;

}

if (image is BitmapSource)

{

MemoryStream stream = new MemoryStream();

encoder.Frames.Add(BitmapFrame.Create(image as BitmapSource));

encoder.Save(stream);

stream.Seek(0, SeekOrigin.Begin);

returnData = new byte[stream.Length];

BinaryReader br = new BinaryReader(stream);

br.Read(returnData, 0, (int)stream.Length);

br.Close();

stream.Close();

}

return returnData;

}

//Method for saving the image data as image in to spec

private static void SaveImageData(byte[] imageData, string filePath)

{

FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(imageData);

bw.Close();

fs.Close();

}


That's it...


...S.VinothkumaR.

Windows Presentation Foundation.

I think all of you getting pretty confusion why need to explain about WPF cause of lot explanation available in more pages. Actually I am going to post some Image manipulation functionalism in upcoming posts. So here is the introduction to WPF.

The Windows Presentation Foundation is a Graphical subsystem in .Net Framework 3.0 which uses a markup language, known as XAML for rich user interface development. WPF formerly code named Avalon. WCF – Indigo, CardSpace – InfoCard, WF – WorkFlow. WPF is included with Windows Vista, Windows Server 2008, Windows XP Service Pack 2 or later and Windows Server 2003.


It provides a clear separation between the user interface and the business logic. A WPF application can be deployed on the desktop or hosted in a web browser. So we can create a wide range of both standalone and browser-hosted applications.


It aims to unify a number of application services: user interface, 2D and 3D drawing, fixed and adaptive documents, advanced typography, vector graphics, raster graphics, animation, data binding, audio and video.


Some examples are Yahoo! Messenger and the New York Times Reader, as well as the Contoso Healthcare Sample Application. WPF is included in the Microsoft .NET Framework, so you can build applications that incorporate other elements of the .NET Framework class library.


I hope this will make you some ideas in WPF. Let we stop the introduction of WPF from here.


...S.VinothkumaR.


Image Resize using C#.

Use the following method to resize an image.

public Image ResizeImage(Image image, int maxWidth, int maxHeight)
{
if (image == null)
{
return image;
}
int width = image.Width;
int height = image.Height;

double widthFactor = (Convert.ToDouble(width) / Convert.ToDouble(maxWidth));
double heightFactor = (Convert.ToDouble(height) / Convert.ToDouble(maxHeight));

if (widthFactor <>
{
// Skip resize
return image;
}
else
{
int newWidth;
int newHeight;
if (widthFactor > heightFactor)
{
newWidth = Convert.ToInt32(Convert.ToDouble(width) / widthFactor);
newHeight = Convert.ToInt32(Convert.ToDouble(height) / widthFactor);
}
else
{
newWidth = Convert.ToInt32(Convert.ToDouble(width) / heightFactor);
newHeight = Convert.ToInt32(Convert.ToDouble(height) / heightFactor);
}
if (newHeight == 0)
{
newHeight = 1;
}
if (newWidth == 0)
{
newWidth = 1;
}
Bitmap bitmap = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bitmap.SetResolution(96, 96);

Graphics graphics = Graphics.FromImage(bitmap);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
image.Dispose();
return bitmap;
}
}


...S.VinothkumaR.

Bytes to GB MB Conversion using C#

Hi all,

Just copy the following method to getting file size with GB or MB formatted.

private string GetFileSize(long Bytes)
{
if (Bytes >= 1073741824)
{
Decimal size = Decimal.Divide(Bytes, 1073741824);
return String.Format("{0:##.##} GB", size);
}
else if (Bytes >= 1048576)
{ Decimal size = Decimal.Divide(Bytes, 1048576);
return String.Format("{0:##.##} MB", size);
}
else if (Bytes >= 1024)
{ Decimal size = Decimal.Divide(Bytes, 1024);
return String.Format("{0:##.##} KB", size);
}
else if (Bytes > 0 & Bytes <>
{ Decimal size = Bytes;
return String.Format("{0:##.##} Bytes", size);
}
else
{
return "0 Bytes";
}
}

...S.VinothkumaR.

Cropping Image

Use the following code to crop an image by C#.

public Image CropImage(Image img, int cropLeft, int cropTop, int cropWidth, int cropHeight)
{
Bitmap bmp = new Bitmap(img);
Rectangle rect = new Rectangle(cropLeft, cropTop, cropWidth, cropHeight);
Bitmap croppedBmp = bmp.Clone(rect, bmp.PixelFormat);
return croppedBmp;
}


....S.VinothkumaR.