The Graphic CenterOrigin is a feature in C# that allows programmers to easily center a graphic or image on a specified origin point. This is extremely useful when working with images that need to be aligned or centered within a certain area.
Code Examples:
Example 1 - Center a Graphic on a Form
// Set up the graphic Graphics graphic = CreateGraphics();
// Get the graphic size SizeF graphicSize = new SizeF(graphic.MeasureString("Hello World");
// Determine the center point of the form PointF centerForm = new PointF(ClientRectangle.Width / 2f, ClientRectangle.Height / 2f);
// Determine the center point of the graphic PointF centerGraphic = new PointF(centerForm.X - graphicSize.Width / 2f, centerForm.Y - graphicSize.Height / 2f);
// Set the graphic origin to the center point graphic.TranslateTransform(centerGraphic.X, centerGraphic.Y);
// Draw the graphic graphic.DrawString("Hello World", Font, Brushes.Black, PointF.Empty);
Example 2 - Center an Image on a PictureBox
// Set up the PictureBox PictureBox pictureBox = new PictureBox();
// Load the image pictureBox.Image = Image.FromFile("example.jpg");
// Get the image size SizeF imageSize = new SizeF(pictureBox.Image.Width, pictureBox.Image.Height);
// Determine the center point of the PictureBox PointF centerPictureBox = new PointF(pictureBox.Width / 2f, pictureBox.Height / 2f);
// Determine the center point of the image PointF centerImage = new PointF(centerPictureBox.X - imageSize.Width / 2f, centerPictureBox.Y - imageSize.Height / 2f);
// Set the image origin to the center point pictureBox.Paint += (sender, e) => e.Graphics.TranslateTransform(centerImage.X, centerImage.Y);
Package/Library:
The Graphic CenterOrigin feature is part of the System.Drawing namespace in the .NET Framework. This means that it is included with the standard installation of C# and does not require any additional package or library to use.
C# (CSharp) Graphic.CenterOrigin - 18 examples found. These are the top rated real world C# (CSharp) examples of Graphic.CenterOrigin extracted from open source projects. You can rate examples to help us improve the quality of examples.