prabha haran

prabha haran

  • NA
  • 81
  • 32.2k

how to set width equally for an image or place it center c#

Mar 18 2017 3:58 AM
hi
 how to add an white space around my reflected image using c#
private static System.Drawing.Image DrawReflection(System.Drawing.Image img, Color toBG, int _Reflectivity) // img is the original image.
{
//This is the static function that generates the reflection...
//int height = img.Height + 100; //Added height from the original height of the image.
// Calculate the size of the new image
int height = (int)(img.Height + (img.Height * ((float)_Reflectivity / 450)));
int Width = (int)(img.Width + (img.Width * ((float)_Reflectivity / 250)));


Bitmap bmp = new Bitmap(Width, height, PixelFormat.Format24bppRgb);


bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);

//Bitmap bmp = new Bitmap(img.Width, height, PixelFormat.Format64bppPArgb); //A new bitmap.
//Brush brsh = new LinearGradientBrush(new Rectangle(0, 0, img.Width + 10, height), Color.Transparent, toBG, LinearGradientMode.Vertical);//The Brush that generates the fading effect to a specific color of your background.
//bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution); //Sets the new bitmap's resolution.
using (Graphics grfx = Graphics.FromImage(bmp)) //A graphics to be generated from an image (here, the new Bitmap we've created (bmp)).
{
// Initialize main graphics buffer
grfx.Clear(toBG);
grfx.DrawImage(img, new Point(0, 0));
grfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle destinationRectangle = new Rectangle(0, img.Size.Height,
img.Width, img.Size.Height);

// Prepare the reflected image
int reflectionHeight = (img.Height * _Reflectivity) / 450;
//int refwidth = (Width / 50);
System.Drawing.Image reflectedImage = new Bitmap(img.Width, reflectionHeight);

// Draw just the reflection on a second graphics buffer
using (Graphics gReflection = Graphics.FromImage(reflectedImage))
{
gReflection.DrawImage(img,
new Rectangle(0,0, reflectedImage.Width, reflectedImage.Height),
0, img.Height - reflectedImage.Height, reflectedImage.Width,
reflectedImage.Height, GraphicsUnit.Pixel);
}
reflectedImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
Rectangle imageRectangle =
new Rectangle(destinationRectangle.X, destinationRectangle.Y,
destinationRectangle.Width,
(destinationRectangle.Height * _Reflectivity) / 450);

// Draw the image on the original graphics
grfx.DrawImage(reflectedImage, imageRectangle);

// Finish the reflection using a gradiend brush
LinearGradientBrush brush = new LinearGradientBrush(imageRectangle,
Color.FromArgb(255 - _Reflectivity, toBG),
toBG, 90, false);
grfx.FillRectangle(brush, imageRectangle);
}
return bmp; //Returns the (bmp) with the generated image.
}
 
 
and my output is

Answers (2)