Click here to Skip to main content
15,900,906 members
Home / Discussions / C#
   

C#

 
QuestionSorting a dataTable by date Pin
kkadir14-Dec-06 21:40
kkadir14-Dec-06 21:40 
AnswerRe: Sorting a dataTable by date Pin
Frank Kerrigan15-Dec-06 0:15
Frank Kerrigan15-Dec-06 0:15 
QuestionHow to get the position of one object in an image? Pin
oliverhuang14-Dec-06 21:40
oliverhuang14-Dec-06 21:40 
QuestionHow to Resize bitmap object in c# Pin
puttaraj14-Dec-06 21:32
puttaraj14-Dec-06 21:32 
AnswerRe: How to Resize bitmap object in c# Pin
Guffa14-Dec-06 21:43
Guffa14-Dec-06 21:43 
GeneralRe: How to Resize bitmap object in c# Pin
puttaraj14-Dec-06 22:30
puttaraj14-Dec-06 22:30 
AnswerRe: How to Resize bitmap object in c# Pin
Guffa14-Dec-06 23:23
Guffa14-Dec-06 23:23 
GeneralRe: How to Resize bitmap object in c# Pin
Ilíon15-Dec-06 9:15
Ilíon15-Dec-06 9:15 
Perhaps the FixedSize() method below will do what you're looking for (I decided to include the other methods in case you find something useful in them too).

For instance:
int fixedWidth = sourceImageObj.Width + 20;
int fixedHeight = sourceImageObj.Height;
Image newImageObj = ImageWork.FixedSize(sourceImageObj, fixedWidth, fixedHeight, backgroundColor);

would return an image object of size ('fixedWidth' * 'fixedHeight') with the original image proportionally scaled to the new size (and centered, with ten extra pixels to either side) and with the "extra" bits set to the value of 'backgroundColor.'
#region Class:  ImageWork
// public class ImageWork
//   public static System.Drawing.Image ClipImage(
//       System.Drawing.Image sourceImage, System.Drawing.Rectangle sourceRect, 
//       int targetWidth, int targetHeight)
//   public static System.Drawing.Image ClipImage(
//       System.Drawing.Image sourceImage, System.Drawing.Rectangle sourceRect, 
//       int targetWidth, int targetHeight, System.Drawing.Color bkgColor)
//   public static System.Drawing.Image ClipImage(
//       System.Drawing.Image sourceImage, System.Drawing.Rectangle sourceRect, 
//       int targetWidth, int targetHeight, System.Drawing.Rectangle targetRect, 
//       System.Drawing.Color bkgColor)
//   
//   public static System.Drawing.Image FixedSize(
//       System.Drawing.Image sourceImage, 
//       int fixedWidth, int fixedHeight, 
//       System.Drawing.Color bkgColor)
//   public static System.Drawing.Image FixedSize_EmptyImage(
//       int fixedWidth, int fixedHeight, System.Drawing.Color bkgColor)
//   public static System.Drawing.Rectangle FixedSize_ResizeRectangle(
//       int sourceWidth, int sourceHeight, 
//       int fixedWidth, int fixedHeight 
//   )
//   public static void FixedSize_ResizeRatios(
//       int sourceWidth, int sourceHeight, 
//       int fixedWidth, int fixedHeight, 
//       ref int targetMapOffsetX, ref int targetMapOffsetY, 
//       ref int targetMapWidth, ref int targetMapHeight, 
//       ref float targetMapRatio 
//   )
//   
public class ImageWork
{
	public static System.Drawing.Image ClipImage(
            System.Drawing.Image sourceImage, 
            System.Drawing.Rectangle sourceRect, 
            int targetWidth, int targetHeight)
	{
		// Return an Image object 
                // (of dimensions 'targetWidth' x 'targetHeight') 
                // clipped from a source Image object
		System.Drawing.Rectangle targetRect = new 
                    System.Drawing.Rectangle(0, 0, targetWidth, targetHeight);
		return ClipImage(sourceImage, sourceRect, 
                    targetWidth, targetHeight, targetRect, 
                    System.Drawing.Color.Empty);
	}

	public static System.Drawing.Image ClipImage(
            System.Drawing.Image sourceImage, 
            System.Drawing.Rectangle sourceRect, 
            int targetWidth, int targetHeight, 
            System.Drawing.Color bkgColor)
	{
		// Return an Image object 
                // (of dimensions 'targetWidth' x 'targetHeight') 
                // clipped from a source Image object
		System.Drawing.Rectangle targetRect = new 
                    System.Drawing.Rectangle(0, 0, targetWidth, targetHeight);
		return ClipImage(sourceImage, sourceRect, 
                    targetWidth, targetHeight, targetRect, 
                    bkgColor);
	}

	public static System.Drawing.Image ClipImage(
            System.Drawing.Image sourceImage, 
            System.Drawing.Rectangle sourceRect, 
            int targetWidth, int targetHeight, 
            System.Drawing.Rectangle targetRect, 
            System.Drawing.Color bkgColor)
	{
		// Return an Image object of dimensions 'targetWidth' x 'targetHeight' 
		// drawn from a clipped region of a source Image object
		// 1.  Create a new Bitmap of dimensions 'targetWidth' x 'targetHeight'
		// 1a. Clear the Bitmap to the Color given as 'bkgColor'
		// 2.  Clip a region of 'sourceImage' as defined by 'sourceRect'
		// 3.  Draw the clipped-region onto the Bitmap at a region defined by 'targetRect'
		// 
		System.Drawing.Bitmap returnImage = new 
                    System.Drawing.Bitmap(
                        targetWidth, targetHeight, 
                        System.Drawing.Imaging.PixelFormat.Format32bppRgb);
		returnImage.SetResolution(
                    sourceImage.HorizontalResolution, 
                    sourceImage.VerticalResolution);

		System.Drawing.Graphics grImage = 
                    System.Drawing.Graphics.FromImage(returnImage);
		grImage.Clear(bkgColor);
		grImage.InterpolationMode = 
                    System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

		grImage.DrawImage(sourceImage, 
			targetRect,
			sourceRect,
			System.Drawing.GraphicsUnit.Pixel);

		grImage.Dispose();
		return returnImage;
	}


	public static System.Drawing.Image FixedSize(
            System.Drawing.Image sourceImage, 
            int fixedWidth, int fixedHeight, 
            System.Drawing.Color bkgColor)
	{
		// Return an Image object (of dimensions 'fixedWidth' x 'fixedHeight') 
		// drawn from a proportionally re-sized source Image object, 
		// with the "undrawn" regions of the new Image cleared to the Color given as 'bkgColor'

                // Compute the target Image offsets/ratios
		System.Drawing.Rectangle sourceRect = new System.Drawing.Rectangle
                    (0, 0, sourceImage.Width, sourceImage.Height);
		System.Drawing.Rectangle targetRect = FixedSize_ResizeRectangle
                    (sourceImage.Width, sourceImage.Height, fixedWidth, fixedHeight);


                // Create the new Image
		return ClipImage(sourceImage, sourceRect, 
                    fixedWidth, fixedHeight, targetRect, bkgColor);
	}

	public static System.Drawing.Image FixedSize_EmptyImage(
            int fixedWidth, int fixedHeight, 
            System.Drawing.Color bkgColor)
	{
		return FixedSize(new System.Drawing.Bitmap(1, 1), 
                    fixedWidth, fixedHeight, bkgColor);
	}

	public static System.Drawing.Rectangle FixedSize_ResizeRectangle(
		int sourceWidth, int sourceHeight, 
		int fixedWidth, int fixedHeight 
	)
	{
		int targetMapOffsetX = 0;
		int targetMapOffsetY = 0;
		int targetMapWidth = 0;
		int targetMapHeight = 0;
		float dummyMapRatio = 0f;
		FixedSize_ResizeRatios(
                    sourceWidth, sourceHeight, 
                    fixedWidth, fixedHeight, 
                    ref targetMapOffsetX, ref targetMapOffsetY,  
                    ref targetMapWidth, ref targetMapHeight, 
                    ref dummyMapRatio);

		return new System.Drawing.Rectangle(
                    targetMapOffsetX, targetMapOffsetY, 
                    targetMapWidth, targetMapHeight);
	}

	public static void FixedSize_ResizeRatios(
		int sourceWidth, int sourceHeight, 
		int fixedWidth, int fixedHeight, 
		ref int targetMapOffsetX, ref int targetMapOffsetY, 
		ref int targetMapWidth, ref int targetMapHeight, 
		ref float targetMapRatio 
	)
	{
		targetMapOffsetX = 0;
		targetMapOffsetY = 0;
		targetMapWidth = 0;
		targetMapHeight = 0;
		targetMapRatio = 0f;

		float targetMapRatioWidth = ((float)fixedWidth/(float)sourceWidth);
		float targetMapRatioHeight = 
                    ((float)fixedHeight/(float)sourceHeight);
		if (targetMapRatioHeight < targetMapRatioWidth)
		{
			targetMapRatio = targetMapRatioHeight;
			targetMapOffsetX = System.Convert.ToInt16(
                            (fixedWidth - (sourceWidth * targetMapRatio))/2);
		}
		else
		{
			targetMapRatio = targetMapRatioWidth;
			targetMapOffsetY = System.Convert.ToInt16(
                            (fixedHeight - (sourceHeight * targetMapRatio))/2);
		}

		targetMapWidth = (int)(sourceWidth * targetMapRatio);
		targetMapHeight = (int)(sourceHeight * targetMapRatio);
	}
}
#endregion 

QuestionAdd Resources for compilation Pin
samtam14-Dec-06 20:27
samtam14-Dec-06 20:27 
GeneralRe: Add Resources for compilation Pin
Paul Conrad24-Dec-07 11:46
professionalPaul Conrad24-Dec-07 11:46 
QuestionReading Security Event Log Pin
JadsHari14-Dec-06 20:23
JadsHari14-Dec-06 20:23 
QuestionPrblm in declaration Pin
Osama12314-Dec-06 20:21
Osama12314-Dec-06 20:21 
AnswerRe: Prblm in declaration Pin
Guffa14-Dec-06 21:55
Guffa14-Dec-06 21:55 
QuestionArray string index Pin
Tomy140214-Dec-06 19:30
Tomy140214-Dec-06 19:30 
AnswerRe: Array string index [modified] Pin
Martin#14-Dec-06 20:34
Martin#14-Dec-06 20:34 
GeneralRe: Array string index Pin
Tomy140214-Dec-06 20:48
Tomy140214-Dec-06 20:48 
GeneralRe: Array string index Pin
Martin#14-Dec-06 20:53
Martin#14-Dec-06 20:53 
GeneralRe: Array string index Pin
Stefan Troschuetz15-Dec-06 0:04
Stefan Troschuetz15-Dec-06 0:04 
GeneralRe: Array string index Pin
Tomy140220-Dec-06 15:36
Tomy140220-Dec-06 15:36 
QuestionOpenGL rendering freezes after a short time Pin
Jasmine250114-Dec-06 18:14
Jasmine250114-Dec-06 18:14 
QuestionCatching an outgoing connection Pin
Andypwnz14-Dec-06 17:42
Andypwnz14-Dec-06 17:42 
QuestionDataTable.GetChanges() Pin
Tomy140214-Dec-06 16:42
Tomy140214-Dec-06 16:42 
AnswerRe: DataTable.GetChanges() Pin
Tomy140214-Dec-06 17:32
Tomy140214-Dec-06 17:32 
QuestionBackgroundWorker question Pin
azusakt14-Dec-06 15:45
azusakt14-Dec-06 15:45 
AnswerRe: BackgroundWorker question Pin
Jasmine250114-Dec-06 18:15
Jasmine250114-Dec-06 18:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.