How to set a 200x200 background of an image? - c#

I am currently creating an app which will let you upload some images and then resize them (100x180) and put a 200x200 white background on them.
I've figured on how to resize the images + create the white background but I cannot figure out how to put them together.
Here is the code for resizing and the white background:
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
private Bitmap DrawFilledRectangle(int x, int y)
{
Bitmap bmp = new Bitmap(x, y);
using (Graphics graph = Graphics.FromImage(bmp))
{
Rectangle ImageSize = new Rectangle(0, 0, x, y);
graph.FillRectangle(Brushes.White, ImageSize);
}
return bmp;
}
Expected result:
Thank you all in advance!

Related

My saved image appears black - Changing opacity of image

im trying to save an image after changing its opacity here is my code:
protected void bntChangeOpacity_Click(object sender, EventArgs e) {
String saveDir = mydir;
Image watermarkImage = Image.FromFile(Server.MapPath(mydir + "imgname.jpg"));
Graphics gr = Graphics.FromImage(watermarkImage);
Rectangle r2 = new Rectangle(new Point(0, 0), new Size(watermarkImage.Width, watermarkImage.Height));
float opacityvalue = 0.5f;
ImageUtils.ImageTransparency.ChangeOpacity(watermarkImage, opacityvalue);
Bitmap b1 = new Bitmap(watermarkImage.Width, watermarkImage.Height);
gr.DrawImage(watermarkImage, r2);
b1.Save(Server.MapPath(saveDir + "sasf.jpg"));
}
the class code is:
public class ImageTransparency {
public static Bitmap ChangeOpacity(Image img, float opacityvalue)
{
Bitmap bmp = new Bitmap(img.Width,img.Height);
Graphics graphics = Graphics.FromImage(bmp);
ColorMatrix colormatrix = new ColorMatrix();
colormatrix.Matrix33 = opacityvalue;
ImageAttributes imgAttribute = new ImageAttributes();
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
graphics.Dispose();
return bmp;
}
}
whats the problem ?? please help
You can do it this way:
private void button1_Click(object sender, EventArgs e)
{
float opacityvalue = 0.5f;
var img= ImageTransparency.ChangeOpacity(Image.FromFile(#"PathToYourImage.png"), opacityvalue);
img.Save(#"PathToYourImage-Opacity.png");
}
class ImageTransparency
{
public static Bitmap ChangeOpacity(Image img, float opacityvalue)
{
Bitmap bmp = new Bitmap(img.Width,img.Height); // Determining Width and Height of Source Image
Graphics graphics = Graphics.FromImage(bmp);
ColorMatrix colormatrix = new ColorMatrix();
colormatrix.Matrix33 = opacityvalue;
ImageAttributes imgAttribute = new ImageAttributes();
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
graphics.Dispose(); // Releasing all resource used by graphics
return bmp;
}
}
Based on Change Opacity of Image in C#

How to draw a image using C# grapics class

I want to draw a image using using C# graphics class using provided X,Y coordinates and zoom Value. I tried to do this but it is not giving me the correct result.
Stream originalStream = ImageHelper.UrlToImageStream(list1.FirstOrDefault().OriginalImageUrl);
var bmp = new Bitmap(bmp.Width, bmp.Height);
int width = 0;
int height = 0;
var img = new Bitmap(bmp,
(int)(bmp.Size.Width / zoomLevel),
(int)(bmp.Size.Height / zoomLevel));
var g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawString(Text, SystemFonts.DefaultFont, Brushes.White, new Rectangle((int)CurrentTextX, (int)CurrentTextY, bmp.Width, bmp.Height));
g.DrawImage(img, new Rectangle((int)CurrentX, (int)CurrentY, bmp.Width, bmp.Height));
var stream = new System.IO.MemoryStream();
img.Save(stream, ImageFormat.Jpeg);
private void mapPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Image img = Image.FromFile(#"C:\img.png");
g.DrawImage(img, new Rectangle(10,10,img.Width/zoomLevel, img.Height/zoomLevel);
}
Try it like this also what is the incorrect result you are getting right now?

C# - Merge Text with Image

I wrote the following code for merging 2 images. My needs were simple, the images will always be the same size so no positioning was needed. I can deal with that later... What I am wondering is, can I modify this to merge text label as my imgFront onto an image, imgBack. The results returned at the end would be a new image that has my text on top.
Is this possible? How?
public static byte[] ImageMerge(Image imgBack, Image imgFront, Int32 width = 200, Int32 height = 200)
{
using (imgBack)
{
using (var bitmap = new Bitmap(width, height))
{
using (var canvas = Graphics.FromImage(bitmap))
{
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.DrawImage(imgBack, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
canvas.DrawImage(imgFront, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
canvas.Save();
}
try
{
return ImageToByte(bitmap);
}
catch (Exception ex)
{
return null;
}
}
}
}
Here's the completed code. I can't believe I didn't share sooner!
public static byte[] ImageTextMerge(Image imgBack, string str, Int32 x, Int32 y, Int32 w, Int32 h, Int32 width = 200, Int32 height = 200)
{
using (imgBack)
{
using (var bitmap = new Bitmap(width, height))
{
using (var canvas = Graphics.FromImage(bitmap))
{
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.DrawImage(imgBack, new Rectangle(0, 0, width, height), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
// Create font and brush
Font drawFont = new Font("Arial", 20);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create rectangle for drawing.
RectangleF drawRect = new RectangleF(x, y, w, h);
// Draw rectangle to screen.
Pen blackPen = new Pen(Color.Transparent);
canvas.DrawRectangle(blackPen, x, y, w, h);
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Near;
// Draw string to screen.
canvas.DrawString(str, drawFont, drawBrush, drawRect, drawFormat);
canvas.Save();
}
try
{
return ImageToByte(bitmap);
}
catch (Exception ex)
{
return null;
}
}
}
}

Bitmap Resize, only crops the image and no resizing

I have this code to resize a bitmap, but all it does is to crop it instead of resizing, what I am doing wrong?
public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
//a holder for the result
Bitmap result = new Bitmap(width, height);
// set the resolutions the same to avoid cropping due to resolution differences
result.SetResolution(image.HorizontalResolution, image.VerticalResolution);
//use a graphics object to draw the resized image into the bitmap
using (Graphics graphics = Graphics.FromImage(result))
{
//set the resize quality modes to high quality
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//draw the image into the target bitmap
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
//return the resulting bitmap
return result;
}
and I call the function like this
bmPhoto = Imaging.ImageProcessing.ResizeImage(bmPhoto, scaledSize.Width, scaledSize.Height);
// Keeping Aspect Ratio
Image resizeImg(Image img, int width)
{
double targetHeight = Convert.ToDouble(width) / (img.Width / img.Height);
Bitmap bmp = new Bitmap(width, (int)targetHeight);
Graphics grp = Graphics.FromImage(bmp);
grp.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
return (Image)bmp;
}
// Without Keeping Aspect Ratio
Image resizeImg(Image img, int width, int height)
{
Bitmap bmp = new Bitmap(width, height);
Graphics grp = Graphics.FromImage(bmp);
grp.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
return (Image)bmp;
}
Try using a Rectangle object to specify the portion of the new image that you want to fill, like so:
graphics.DrawImage(image, new Rectangle(0, 0, result.Width, result.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, null);
As noted the Rectangle specifies that the image should be drawn between the top left and bottom right corner, and then you provide the coordinates of the original image that you want to scale into that area (0,0,image.Width,image.Height).

C#: how to take a screenshot of a portion of screen

like
TakeScreenshot(new Rectangle(0,0,100,100), "output.jpg");
Use the following:
Rectangle rect = new Rectangle(0, 0, 100, 100);
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy);
bmp.Save(fileName, ImageFormat.Jpeg);
Here is the code to capture the screen. Change the values to the size you need.
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
printscreen.Save(#"C:\printscreen.jpg", ImageFormat.Jpeg);
Or make method which will return you captured image like this :
Image CaptureScreen(int sourceX, int sourceY, int destX, int destY,
Size regionSize)
{
Bitmap bmp = new Bitmap(regionSize.Width, regionSize.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(sourceX, sourceY, destX, destY, regionSize);
return bmp;
}
......
// call
Image image = CaptureScreen(sourceX, sourceY, destX, destY, regionSize);
image.Save(#"C:\Somewhere\screen.jpg);
Use the Graphics.CopyFromScreen method. Google turns up this tutorial.
Have you checked the Graphics.CopyFromScreen method?

Categories