I would like to add to picture black box. How to do it?
using (var image = Image.FromFile(imagePath))
{
using (var newImage = ScaleImage(image, 300, 400))
{
int width = newImage.Size.Width;
int height = newImage.Size.Height;
Graphics DrawingSurface = Graphics.FromImage(newImage);
//graphics.DrawRectangle(); ????
newImage.Save(imagePathDuzy);
}
}
Use one of the FillRectangle methods on the Graphics type: http://msdn.microsoft.com/en-us/library/yysstebh.aspx
Probably something like this:
DrawingSurface.FillRectangle(new SolidBrush(Colors.Black), new Rectangle(0, height - 100, width, height));
This shold draw a black rectangle with 100px height at the bottom of the image.
Related
I want to show a Resized Picture in my Picturebox.
The original picture is:
And the Picture in my form:
My picturebox size is 500x500px.
My method that I use for the resize:
public static Image ResizePicByWidth(Image sourceImage, double newWidth)
{
double sizeFactor = newWidth / sourceImage.Width;
double newHeigth = sizeFactor * sourceImage.Height;
Bitmap newImage = new Bitmap((int)newWidth, (int)newHeigth);
using (Graphics g = Graphics.FromImage(newImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourceImage, new Rectangle(0, 0, (int)newWidth, (int)newHeigth));
}
return newImage;
}
I call the method with the original picture and the width from the picturebox.
But how can i resize the picture correctly?
I want my Form to show the whole Picture.
PictureBox has a SizeMode property. If you set this to Zoom it will automatically resize the image in it to fit inside it.
Below is my code for changing my panel information into a Bitmap.
The Bitmap is first generated by my panel information and then saved to as a image file.
I confirmed that the width, height, and bounds represent the right information given by my panel.
I am currently unsure of why my result bmp/jpeg file is different from the image on my panel.
//bitmap saving function
Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height);
Debug.WriteLine("bounds: " + panel1.ClientRectangle);
this.panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
bmp.Save(#"C:\Documents and Settings\Flaw\Desktop\Test.bmp", ImageFormat.Bmp);
//drawing function
System.Drawing.Graphics graphicsObj;
graphicsObj = this.panel1.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Black, 5);
graphicsObj.Clear(Color.White);
//graphicsObj.DrawLine(myPen, 50, 50, 100, 100);
if (bCircle)
{
graphicsObj.DrawEllipse(myPen, x, y, 100, 100);
}
else if (bSquare)
{
graphicsObj.DrawRectangle(myPen, x, y, 100, 100);
}
The result I get by saving the bitmap.
The image that is on my panel1 (cropped from my Window Form)
Your Bounds property is the relationship the panel has to the parent container, so this won't always work:
this.panel1.DrawToBitmap(bmp, panel1.Bounds);
Try this instead:
this.panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
Your bitmap size should also use the ClientSize properties, since the panel's Width and Height properties include any border sizes:
Bitmap bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height);
Per your update, CreateGraphics is a temporary canvas and won't be a part of the panel, so there was nothing to save. Use the panel's paint event instead:
private void panel1_Paint(object sender, PaintEventArgs e) {
using (Pen myPen = new Pen(Color.Black, 5)) {
e.Graphics.Clear(Color.White);
if (bCircle) {
e.Graphics.DrawEllipse(myPen, x, y, 100, 100);
} else if (bSquare) {
e.Graphics.DrawRectangle(myPen, x, y, 100, 100);
}
}
}
To make an update, you just need to invalidate the control:
panel1.Invalidate();
I'm trying to draw a part of a .png image but the code i found is not working. this is my code
//define canvas
canvas = pb.CreateGraphics();
sPicture = new Bitmap(pb.Width, pb.Height);
sCanvas = Graphics.FromImage(sPicture);
// Create a Bitmap object from a file.
Image image = Image.FromFile(#"");
// Clone a portion of the Bitmap object.
Rectangle cloneRect = new Rectangle(0, 0, 11, 6);
System.Drawing.Imaging.PixelFormat format =
image.PixelFormat;
Image cloneBitmap = image.Clone(cloneRect, format); //Error: No overload for method 'Clone' takes2 arguments
// Draw the cloned portion of the Bitmap object.
canvas.DrawImage(cloneBitmap, 0, 0);
This is for a sprite sheet and thanks.
You don't need to use Clone(), you can do this directly with Graphics.DrawImage(). It looks like you are trying to do this in WinForms. If, so handle OnPaint for the control you want to draw on. In the example below I'm drawing directly on the form.
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
int width = 60;
int height = 60;
// Create a Bitmap object from a file.
Image sourceImage = Image.FromFile(#"C:\Users\Mike\Downloads\logo.png");
// Draw a portion of the source image.
Rectangle sourceRect = new Rectangle(0, 0, width, height);
graphics.DrawImage(sourceImage, 0, 0, sourceRect, GraphicsUnit.Pixel);
}
If you want to do this without WinForms, there is the extra step of creating the target Graphics instance.
int width = 60;
int height = 60;
// Create a Bitmap object from a file.
Image sourceImage = Image.FromFile(#"C:\Users\Mike\Downloads\logo.png");
// Create a drawing target
Bitmap bitmap = new Bitmap(width, height, sourceImage.PixelFormat);
Graphics graphics = Graphics.FromImage(bitmap);
// Draw a portion of the source image.
Rectangle sourceRect = new Rectangle(0, 0, width, height);
graphics.DrawImage(sourceImage, 0, 0, sourceRect, GraphicsUnit.Pixel);
// Save
bitmap.Save(#"C:\Users\Mike\Downloads\out.png");
I am trying to resize an image (bitmap) in C# without stretching the image.
Say the image is 100x100 pixels.
I am looking to make it 100x110 pixels, and leave a white gap at the bottom of the image where it added the extra pixels.
I have done this, but cannot find a way to specify the pixel format. I need it to be 8bppindexed. I've attached an example to show the before and after image.
Here is the code I have so far.
string visit2 = "C:\\users\\moorez\\desktop\\visit2.bmp";
Bitmap orig = new Bitmap(visit2);
int width = orig.Width;
int height = orig.Height;
int newHeight = height + 2;
Bitmap newImage = orig.Clone(new Rectangle(0, 0, width, height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
newImage.Save("C:\\users\\moorez\\desktop\\visit3.bmp");
Bitmap test = new Bitmap(width, newHeight);
Graphics g = Graphics.FromImage(test);
g.DrawImage(newImage, new Point(0, 0));
test.Save("C:\\users\\moorez\\desktop\\visit4.bmp");
You can try this
Bitmap bmp = new Bitmap(newImage.Width, newHeight);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
g.DrawImageUnscaled(newImage, 0, 0, newImage.Width, newHeight);
bmp.Save(#"C:\\users\\moorez\\desktop\\visit3.bmp", ImageFormat.Jpeg);
My scenario:
I have one color background image JPG.
I have one black text on white background JPG.
Both images are the same size (height and width)
I want to overlay the image with black text and white background over the color background image, i.e. the white background becomes transparent to see the color background beneath it.
How can I do this with GDI in C#?
Thanks!
Thanks to GalacticCowboy I was able to come up with this solution:
using (Bitmap background = (Bitmap)Bitmap.FromFile(backgroundPath))
{
using (Bitmap foreground = (Bitmap)Bitmap.FromFile(foregroundPath))
{
// check if heights and widths are the same
if (background.Height == foreground.Height & background.Width == foreground.Width)
{
using (Bitmap mergedImage = new Bitmap(background.Width, background.Height))
{
for (int x = 0; x < mergedImage.Width; x++)
{
for (int y = 0; y < mergedImage.Height; y++)
{
Color backgroundPixel = background.GetPixel(x, y);
Color foregroundPixel = foreground.GetPixel(x, y);
Color mergedPixel = Color.FromArgb(backgroundPixel.ToArgb() & foregroundPixel.ToArgb());
mergedImage.SetPixel(x, y, mergedPixel);
}
}
mergedImage.Save("filepath");
}
}
}
}
Works like a charm. Thanks!
If the images are the same size, iterate over them and "AND" the colors for each pixel. For the white pixels, you should get the color of the other image, and for the black ones you should get black.
If they're not the same size, scale first.
I'm making this up off the top of my head, but something like:
Color destColor = Color.FromArgb(pixel1.ToArgb() & pixel2.ToArgb());
There exist easier and faster way. You should use ImageAttributes when you draw image that must be partially visible.
Image BackImage = Image.FromFile(backgroundPath);
using (Graphics g = Graphics.FromImage(BackImage))
{
using (ForeImage = Image.FromFile(foregroundPath))
{
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(Color.FromArgb(245, 245, 245), Color.FromArgb(255, 255, 255),
ColorAdjustType.Default);
g.DrawImage(ForeImage, new Rectangle(0, 0, BackImage.Width, BackImage.Height),
0, 0, BackImage.Width, BackImage.Height, GraphicsUnit.Pixel, imageAttr);
}
}
SetColorKey method will make color from specified range transparent, so you can make your white bitmap pixels transparent, including all pixels that are affected to jpeg compression artefacts.