I have an image with the dimensions of 100x50 and I want to draw a dot in the center - i.e. at 50x25. How would I do this?
you could use the setPixle() function.
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.Black, new Rectangle(50, 25, 1, 1));
look here for saving the picture
it does not draw on form load so you should add your code in form paintevent :
private void Form1_Paint(object sender, PaintEventArgs e)
{
Application.DoEvents();
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.DarkBlue, new Rectangle(120, 90, 1, 1));
}
Image img = pictureBox1.Image;
Graphics g = Graphics.FromImage(img);
g.DrawEllipse(Pens.DarkBlue, new Rectangle(50, 25, 1, 1));
g.DrawImage(img, new Point(0, 0));
img.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
try this for other question :
Rectangle bounds = new Rectangle(10, 20, 50, 60);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(Point.Empty,Point.Empty, bounds.Size);
bitmap.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
this code will capture screen with bound of bounds rectangle.
Related
I need to draw over a bitmap loaded in a PictureBox. I've been looking for a way on this site but the answers don't work because the resulting image is in a wrong location.
private void pbImage_MouseClick(object sender, MouseEventArgs e)
{
currentImage = pbImage.Image;
Bitmap imageAux = new Bitmap(currentImage.Width, currentImage.Height);
using (Graphics g = Graphics.FromImage(imageAux))
{
Brush brush = new SolidBrush(Color.Red);
Pen pen = new Pen(Color.Black, 10);
g.DrawImage(currentImage, pbImage.Location);
g.Flush();
if (e.Button == MouseButtons.Left)
{
Control control = (Control)sender;
punto = ((Control)sender).PointToScreen(new Point(e.X, e.Y)); ;
g.DrawRectangle(pen, new Rectangle(punto.X, punto.Y, 50, 50));
g.FillRectangle(brush, new Rectangle(punto.X, punto.Y, 50, 50));
g.Flush();
g.Dispose();
pbImage.Invalidate();
}
}
pbImage.Refresh();
currentImage.Save("C:\\prueba3.bmp");
imageAux.Save("C:\\prueba4.bmp");
pbImage.Image = (Image)imageAux.Clone();
}
You should create additional Bitmaps to draw.
Load the original in a Bitmap.
Create a bitmap to assign to the picturebox. (backpage)
Create/Generate a grayscale version of the original bitmap.
When it needs to draw:
Clear the picturebox bitmap
Draw the grayscale onto
Draw the 50, 50 rect of the original on the selection location in the picturebox bitmap
Refresh het picturebox
Here's an example, Add a picturebox to the Form and replace the filename with your image filename.
public partial class Form1 : Form
{
private Bitmap _originalBitmap;
private Bitmap _grayScaleBitmap;
private Bitmap _pictureBoxBitmap;
//private Brush brush = new SolidBrush(Color.Red);
private Pen pen = new Pen(Color.Black, 1);
private Point? _inspectRectStart;
public Form1()
{
InitializeComponent();
_originalBitmap = (Bitmap)Bitmap.FromFile("lin-png-256x256-Rafael_256x256_png-256x256.png");
_grayScaleBitmap = MakeGrayscale3(_originalBitmap);
_pictureBoxBitmap = new Bitmap(_originalBitmap.Width, _originalBitmap.Height);
DrawImage();
pictureBox1.Image = _pictureBoxBitmap;
}
private static Bitmap MakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height);
//get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap);
//create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
//dispose the Graphics object
g.Dispose();
return newBitmap;
}
private void DrawImage()
{
using (Graphics g = Graphics.FromImage(_pictureBoxBitmap))
{
g.Clear(Color.White); // if the bitmap has transparent parts
// draw the grayscale image.
g.DrawImage(_grayScaleBitmap, new Rectangle(0, 0, _originalBitmap.Width, _originalBitmap.Height), 0, 0, _originalBitmap.Width, _originalBitmap.Height, GraphicsUnit.Pixel);
// if there is a selection, draw it.
if (_inspectRectStart.HasValue)
{
var rect = new Rectangle(_inspectRectStart.Value, new Size(50, 50));
// draw the original bitmap in a rectangle.
g.DrawImage(_originalBitmap, _inspectRectStart.Value.X, _inspectRectStart.Value.Y, rect, GraphicsUnit.Pixel);
g.DrawRectangle(pen, rect);
}
}
pictureBox1.Refresh();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// move the rectangle position
_inspectRectStart = e.Location;
// redraw image.
DrawImage();
}
}
I think, thats because g.DrawImage(currentImage, pbImage.Location); will draw the image relative on the imageAux with the actual location of the pbImage in relation to it's parent. try new Point(); instead.
I have this code:
Bitmap newbmp = new Bitmap(512, 512);
foreach (Point s in CommonList)
{
w.WriteLine("The following points are the same" + s);
newbmp.SetPixel(s.X, s.Y, Color.Red);
}
w.Close();
newbmp.Save(#"c:\newbmp\newbmp.bmp", ImageFormat.Bmp);
newbmp.Dispose();
The code is not in a paint event.
Then I have a paint event of a pictureBox1:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (cloudPoints != null)
{
if (DrawIt)
{
e.Graphics.DrawRectangle(pen, rect);
pointsAffected = cloudPoints.Where(pt => rect.Contains(pt));
CloudEnteringAlert.pointtocolorinrectangle = pointsAffected.ToList();
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format32bppArgb);
CloudEnteringAlert.Paint(e.Graphics, 1, 200, bmp);
}
}
}
In the paint event I'm drawing a rectangle. The variable rect.
I want to draw the rectangle rect on the newbmp. After saving the newbmp to draw this rect on him.
How can I draw the rect on the newbmp ?
You should create a Graphics object from the bitmap to work on it:
..
Bitmap newbmp = new Bitmap(512, 512);
foreach (Point s in CommonList)
{
Console.WriteLine("The following points are the same" + s);
newbmp.SetPixel(s.X, s.Y, Color.Red);
}
using (Graphics G = Graphics.FromImage(newbmp))
{
G.DrawRectangle(Pens.Red, yourRectangle);
..
}
newbmp.Save(#"c:\newbmp\newbmp.bmp", ImageFormat.Bmp);
newbmp.Dispose();
For other Pen properties like Width or LineJoin use this format:
using (Graphics G = Graphics.FromImage(newbmp))
using (Pen pen = new Pen(Color.Red, 8f) )
{
// rounded corners please!
pen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
G.DrawRectangle(pen, yourRectangle);
//..
}
I have Bitmap image:
I draw rectangle on that image:
Bitmap myImage = new Bitmap("path");
using (Graphics gr = Graphics.FromImage(myImage))
{
Pen pen = new Pen(Color.Black, 2);
gr.DrawRectangle(pen, 100,100, 100, 200);
}
I want to fill the entire image with black color, except the rectangle.
Like this:
Any idea how to implement it?
A simple ExcludeClip will do:
using (Graphics g = Graphics.FromImage(myImage)) {
g.ExcludeClip(new Rectangle(100, 100, 100, 200));
g.Clear(Color.Black);
}
Can someone tell me what i don't make as it should, why i cannot save the drawing to a physical storage?
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Red, 3);
Bitmap bmp = new Bitmap(700, 900);
Graphics gr = this.CreateGraphics();
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
//when i do this way it saves only a black rectangle, without other drawn content
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
// If i use the following 2 commented lines it saves only a empty rectangle.
//Rectangle rec = new Rectangle(0, 0, 700, 900);
// panel1.DrawToBitmap(bmp, rec);
}
Thank you for advice!
You have two problems here.
Drawing your panel's contents. This should be done inside its Paint event handler, like this:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
{
// get the panel's Graphics instance
Graphics gr = e.Graphics;
// draw to panel
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
}
}
Saving your panel's contents as an image. This part should be done somewhere else (for example, when you click on a "Save" button):
private void saveButton_Click(object sender, EventArgs e)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
}
}
The instance gr has nothing to do with your bitmap (bmp). So you're creating graphics that are associated with the form or control, and have a separate bitmap. When you save the bitmap, you haven't drawn anything in it.
You need to get a Graphics object from the Image, not from your form. I have not tested this, but it should work.
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
using (Bitmap bmp = new Bitmap(700, 900))
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
}
}
When I try to draw an image over another one using Graphics library C# it scale the small one and cover the first one:
public Form1()
{
//InitializeComponent();
read_file();
InitializeComponent1();
SetStyle(ControlStyles.Opaque, true);
// theImage = new Bitmap("F:/4th year/1st Term/sensor network/proj/reconstructscene/reconstructscene/images/tryImage.jpg");
theImage2 = new Bitmap("F:/4th year/1st Term/sensor network/proj/reconstructscene/reconstructscene/images/1.jpg");
// theImage = new Bitmap(newImage);
theImage = new Bitmap("F:/4th year/1st Term/sensor network/proj/reconstructscene/reconstructscene/images/tryImage.jpg");
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
//e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(theImage, ClientRectangle);
// Create pen.
g.FillRectangle(new SolidBrush(Color.Red), 50, 50, 50, 50);
RectangleF recto = new System.Drawing.RectangleF(50, 50, 50, 50);
Pen blackPen = new Pen(Color.Black,1);
g.DrawRectangle(blackPen, 50, 50, 50, 50);
g.DrawImage(theImage2, ClientRectangle); //this will cover the 1st one
}
Have this instead:
g.DrawImage(theImage2, 0, 0, theImage2.Width, theImage2.Height);
This should draw the image in the "proper" place without stretching it.
look at draw on image
insteed DrawString you should use DrawImage