why using this snippet does not draw an image on the screen? there are other commands to draw before this line they work fine. for example, there some rectangle and fill shape command that works.
Graphics imgGB = e.Graphics;
imgGB.DrawImage(
Properties.Resources.logo,
new Rectangle(
new Point(DoorGlassFill.Left, DoorPrimeter.Bottom + Drawing_HandleHeight),
new Size(200,200)));
the whole method is:
//draw door
Graphics DoorGP = e.Graphics;
Rectangle DoorFrame = new Rectangle(new Point(DoorPrimeter.Left + Drawing_LeftDis + Drawing_Off, DoorPrimeter.Top + TopDistance + (2*Drawing_Off)+Drawing_TopBlockHeight), new Size(Drawing_DoorWidth,DoorPrimeter.Height-TopDistance-Drawing_TopBlockHeight-(2*Drawing_Off)));
DoorGP.DrawRectangle(pen, DoorFrame);
Rectangle GP113 = new Rectangle(new Point(DoorFrame.Left+Drawing_GP113,DoorFrame.Top+Drawing_GP113), new Size(DoorFrame.Width-(2*Drawing_GP113),DoorFrame.Height-Drawing_GP113));
DoorGP.DrawRectangle(pen, GP113);
Rectangle DoorGlass = new Rectangle(new Point(GP113.Left+Drawing_GP119,GP113.Top+Drawing_GP119), new Size(GP113.Width-(2*Drawing_GP119),GP113.Height-Drawing_GP119));
Graphics GlassFillGP = e.Graphics;
Rectangle DoorGlassFill = new Rectangle(new Point(GP113.Left + Drawing_GP119, GP113.Top + Drawing_GP119), new Size(GP113.Width - (2 * Drawing_GP119), GP113.Height - (2 * Drawing_GP119)));
GlassFillGP.FillRectangle(new LinearGradientBrush(DoorGlassFill, Color.AliceBlue, Color.LightSkyBlue, 45f), DoorGlassFill);
DoorGP.DrawRectangle(pen, DoorGlass);
Graphics imgGB = e.Graphics;
imgGB.DrawImage(Properties.Resources.logo__sajiran1,new Rectangle(new Point(DoorGlassFill.Left, DoorPrimeter.Bottom + Drawing_HandleHeight),new Size(200,200)));
tried this too:
//draw door
Graphics DoorGP = e.Graphics;
Rectangle DoorFrame = new Rectangle(new Point(DoorPrimeter.Left + Drawing_LeftDis + Drawing_Off, DoorPrimeter.Top + TopDistance + (2*Drawing_Off)+Drawing_TopBlockHeight), new Size(Drawing_DoorWidth,DoorPrimeter.Height-TopDistance-Drawing_TopBlockHeight-(2*Drawing_Off)));
DoorGP.DrawRectangle(pen, DoorFrame);
Rectangle GP113 = new Rectangle(new Point(DoorFrame.Left+Drawing_GP113,DoorFrame.Top+Drawing_GP113), new Size(DoorFrame.Width-(2*Drawing_GP113),DoorFrame.Height-Drawing_GP113));
DoorGP.DrawRectangle(pen, GP113);
Rectangle DoorGlass = new Rectangle(new Point(GP113.Left+Drawing_GP119,GP113.Top+Drawing_GP119), new Size(GP113.Width-(2*Drawing_GP119),GP113.Height-Drawing_GP119));
Graphics GlassFillGP = e.Graphics;
Rectangle DoorGlassFill = new Rectangle(new Point(GP113.Left + Drawing_GP119, GP113.Top + Drawing_GP119), new Size(GP113.Width - (2 * Drawing_GP119), GP113.Height - (2 * Drawing_GP119)));
GlassFillGP.FillRectangle(new LinearGradientBrush(DoorGlassFill, Color.AliceBlue, Color.LightSkyBlue, 45f), DoorGlassFill);
DoorGP.DrawRectangle(pen, DoorGlass);
Bitmap img = Properties.Resources.Handle_Meroni;
Graphics imgGB = e.Graphics;
img.SetResolution(imgGB.DpiX, imgGB.DpiY);
imgGB.DrawImage(img,new Rectangle(new Point(DoorGlassFill.Left, DoorPrimeter.Bottom + Drawing_HandleHeight),new Size(200,200)));
Related
The question is simple, assuming you have a shape (lets say a rectangle) and a text ("hello" for example), so it'll write the text all accross the borders of the rectangle for as many times as it fits, for example:
hello hello hello hello
hello hello
hello hello
hello hello hello hello
In order to do it I assume you'd need to use a graphics variable, I just dont know how to do it.
A code for drawing a string in a bitmap object:
Bitmap tempp = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(tempp);
SizeF w = g.MeasureString("22", new Font("Tahoma", 200));//in order to get the size of the string as a pixel measurement
Bitmap bmp = new Bitmap((int)w.Width+1, (int)w.Height+1);//the bitmap that will contain the text as a picture
RectangleF rectf = new RectangleF(0, 0, (int)w.Width+1, (int)w.Height+1);
g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
g.DrawString("22", new Font("Tahoma", 200), Brushes.Black, rectf, format);
g.Flush();
Thanks in advance.
For second comment:
hello hello hello
hel llo
hel llo
hello hello hello
I hope this is what you need. There isn't much to explain here. The logic is pretty straightforward.
public string MyString = "Hello"
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
var strFont = new Font("Tahoma", 10);
var strSizeF = g.MeasureString(MyString, strFont);
var canvas = new Rectangle
{
Height = 200,
Width = 200,
Location = new Point(10, 10),
};
g.DrawRectangle(new Pen(new SolidBrush(Color.Blue)), canvas);
var nx = (int)(canvas.Width / strSizeF.Width);
var ny = (int)(canvas.Height / strSizeF.Height);
var spacingX = (canvas.Width - nx * strSizeF.Width) / (nx-1);
var spacingY = (canvas.Height - ny * strSizeF.Height) / (ny-1);
//draw top row and bottom row
int i;
for (i = 0; i < nx; i++)
{
g.DrawString(
MyString,
strFont,
Brushes.Black,
new PointF(canvas.X + i*(strSizeF.Width + spacingX), canvas.Y)
);
g.DrawString(
MyString,
strFont,
Brushes.Black,
new PointF(canvas.X + i * (strSizeF.Width + spacingX), canvas.Y + canvas.Height - strSizeF.Height)
);
}
//divide the string into half
var isLengthOdd = MyString.Length % 2 != 0;
var substr1 = MyString.Substring(0, MyString.Length / 2 + (isLengthOdd ? 1 : 0));
var substr2 = MyString.Substring(MyString.Length / 2, MyString.Length - MyString.Length / 2);
var substr2SizeF = g.MeasureString(substr2, strFont);
//draw side rows
for (i = 1; i < ny - 1; i++)
{
g.DrawString(
substr1,
strFont,
Brushes.Black,
new PointF(canvas.X, canvas.Y + i * (strSizeF.Height + spacingY))
);
g.DrawString(
substr2,
strFont,
Brushes.Black,
new PointF(canvas.X + canvas.Width - substr2SizeF.Width, canvas.Y + i * (strSizeF.Height + spacingY))
);
}
}
Result:
I've run into an issue with drawing text and basic graphic drawing operations not having the proper placement & quality when the drawing matrix has large offset values. I've tried number SmoothMode, InterpolationMode, & TextRenderingHint options with no luck.
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public void RenderImageClosePointDrawing()
{
float x = 68336, y = 99460;
PointF anchorPoint = new PointF(17494176, 25461836);
PointF anchorPoint2= new PointF(17494076, 25461836);
string textLabel = "9318";
float textFontSize = 20;
float symbolsize = 34;
string fontFamly = "Arial";
Bitmap bitmap = new Bitmap(256, 256);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphics.Transform = new Matrix(1, 0, 0, 1, -x * 256, -y * 256);
//Draw the circle
Pen polyPen = new Pen(new SolidBrush(Color.Black), 2);
Brush polyBrush = new SolidBrush(Color.Teal);
graphics.DrawEllipse(polyPen, anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
graphics.FillEllipse(polyBrush, anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
RectangleF drawnArea = new RectangleF(anchorPoint.X, anchorPoint.Y, symbolsize, symbolsize);
Pen polyPen2 = new Pen(new SolidBrush(Color.Black), 1);
Brush polyBrush2 = new SolidBrush(Color.Teal);
graphics.DrawEllipse(polyPen2, anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
graphics.FillEllipse(polyBrush2, anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
RectangleF drawnArea2 = new RectangleF(anchorPoint2.X, anchorPoint2.Y, symbolsize, symbolsize);
Pen polyPen3 = new Pen(new SolidBrush(Color.Red), 1);
graphics.DrawRectangle(polyPen3, drawnArea.X, drawnArea.Y, drawnArea.Width, drawnArea.Height);
graphics.DrawRectangle(polyPen3, drawnArea2.X, drawnArea2.Y, drawnArea2.Width, drawnArea2.Height);
//Draw the text
Pen textOutlinePen = new Pen(new SolidBrush(Color.Orange), (float)4);
textOutlinePen.EndCap = LineCap.Round;
textOutlinePen.LineJoin = LineJoin.Round;
textOutlinePen.MiterLimit = 0;
Brush textFillBrush = new SolidBrush(Color.Teal);
FontFamily textFontFamily = new FontFamily(fontFamly);
PointF textAnchor = new PointF(anchorPoint.X, anchorPoint.Y);
ShiftTextAnchor_NW(textLabel, textFontSize, ref drawnArea, textFontFamily, ref textAnchor);
var textPath = new GraphicsPath();
textPath.AddString(textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor,
new StringFormat()
);
graphics.DrawPath(textOutlinePen, textPath);
graphics.FillPath(textFillBrush, textPath);
//Draw the text2
Pen textOutlinePen2 = new Pen(new SolidBrush(Color.Orange), (float)1);
textOutlinePen.EndCap = LineCap.Round;
textOutlinePen.LineJoin = LineJoin.Round;
textOutlinePen.MiterLimit = 0;
PointF textAnchor2 = new PointF(anchorPoint2.X, anchorPoint2.Y);
ShiftTextAnchor_NW(textLabel, textFontSize, ref drawnArea2, textFontFamily, ref textAnchor2);
var textPath2 = new GraphicsPath();
textPath2.AddString(textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor2,
new StringFormat()
);
graphics.DrawPath(textOutlinePen2, textPath2);
graphics.FillPath(textFillBrush, textPath2);
}
bitmap.Save(#"C:\ClosePointDrawing.png", ImageFormat.Png);
}
private static void ShiftTextAnchor_NW(string textLabel, float textFontSize, ref RectangleF drawnArea, FontFamily textFontFamily, ref PointF textAnchor)
{
GraphicsPath tempPath = new GraphicsPath();
tempPath.AddString(
textLabel,
textFontFamily,
(int)FontStyle.Bold,
textFontSize,
textAnchor,
new StringFormat()
);
var textBounds = tempPath.GetBounds();
var offsetX = textBounds.X - textAnchor.X;
var offsetY = textBounds.Y - textAnchor.Y;
textAnchor = new PointF(drawnArea.Left - (textBounds.Width + offsetX), drawnArea.Top - (textBounds.Height + offsetY));
}
When you run this code you'll get this for output: ClosePointDrawing.png
You'll notice that the text doesn't not look nice (distorted some) and also that the Black stroke outline around the circle is only lined up properly with its Teal color filled circle with the one on the left when using a 1 pixel stroke. The one on the right uses a 2 pixel stroke. You'll also see that the Red Squares are not lined up with the Teal Circle, they should be completely encapsulating it.
Now if you change the first few values in the code so that it doesn't use a large offset as follows:
float x = 0, y = 0;
PointF anchorPoint = new PointF(150, 50);
PointF anchorPoint2 = new PointF(50, 50);
You'll get this for output: ClosePointDrawing2.png
Notice that the Text looks much better, and that the strokes are perfectly lined up with the filled circles as well as the red squares.
Is there anything that can be done, so that it will render it properly with the larger matrix?
I am trying to draw a rectangle into a panel and then save this into a bitmap. the screen shows that I have been successful drawing the rectangles but when I view the saved bitmaps, they are empty white rectangles.
System.Drawing.Graphics graphicsObj;
graphicsObj = panel1.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Black, 1);
System.Drawing.Rectangle myRectangle = new System.Drawing.Rectangle(x, y, 50, 50);
greenBrush = new SolidBrush(Color.MediumVioletRed);
graphicsObj.FillRectangle(greenBrush, myRectangle);
graphicsObj.DrawString("SomeString", new System.Drawing.Font
("Arial", 12), Brushes.Blue, myRectangle);
graphicsObj.DrawRectangle(myPen, myRectangle);
string dir = Directory.GetCurrentDirectory();
string img = t.Field<int>("ID").ToString() + ".png";
string total = dir + "\\pics\\" + img;
Bitmap bmp = new Bitmap(panel1.ClientRectangle.Width,
panel1.ClientRectangle.Height);
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
string dira = Directory.GetCurrentDirectory();
string imga = t.Field<int>("ID").ToString() + "cal.bmp";
string totala = dira + "\\pics\\" + imga;
bmp.Save(totala);
When I load a transparent png image with ImageBrush in WPF Rectangle background, I get a white background behind the image. It hides all other content back to that image.
public void DisplayModel()
{
Uri url = new Uri("pack://application:,,,/Model/01.png");
BitmapImage bmpimg = new BitmapImage();
bmpimg.BeginInit();
bmpimg.UriSource = url;
bmpimg.EndInit();
ImageBrush BrushView = new ImageBrush(bmpimg);
BrushView.AlignmentX = AlignmentX.Right;
BrushView.Stretch = Stretch.Fill;
Material MaterialView = new DiffuseMaterial(BrushView);
MeshGeometry3D mesh_view = new MeshGeometry3D();
double Zdeep = -48;
double x = -30;
double y = -5;
double xEnd = 60;
double yEnd = 45;
//MessageBox.Show(x.ToString()+"-"+y.ToString());
commonObject.makeRectangle(mesh_view, new Point3D(x, y, Zdeep), new Point3D(x + xEnd, y, Zdeep), new Point3D(x + xEnd, yEnd, Zdeep), new Point3D(x, yEnd, Zdeep), new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 0));
GeometryModel3D ViewGeometry = new GeometryModel3D(mesh_view, MaterialView);
group.Children.Add(ViewGeometry);
}
Here code I used and get this kind of out put:
I have got a System.Drawing.Bitmap in my code.
The width is fix, the height varies.
What I want to do, is to add a white border around the bitmap, with about 20 pixel, to all 4 edges.
How would this work?
You could draw a rectangle behind the bitmap. The width of the rectangle would be (Bitmap.Width + BorderWidth * 2), and the position would be (Bitmap.Position - new Point(BorderWidth, BorderWidth)). Or at least that's the way I'd go about it.
EDIT:
Here is some actual source code explaining how to implement it (if you were to have a dedicated method to draw an image):
private void DrawBitmapWithBorder(Bitmap bmp, Point pos, Graphics g) {
const int borderSize = 20;
using (Brush border = new SolidBrush(Color.White /* Change it to whichever color you want. */)) {
g.FillRectangle(border, pos.X - borderSize, pos.Y - borderSize,
bmp.Width + borderSize, bmp.Height + borderSize);
}
g.DrawImage(bmp, pos);
}
You can use 'SetPixel' method of a Bitmap class, to set nesessary pixels with the color. But more convenient is to use 'Graphics' class, as shown below:
bmp = new Bitmap(FileName);
//bmp = new Bitmap(bmp, new System.Drawing.Size(40, 40));
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 0), new Point(0, 40));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 0), new Point(40, 0));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 40), new Point(40, 40));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(40, 0), new Point(40, 40));
Below function will add border around the bitmap image. Original image will increase in size by the width of border.
private static Bitmap DrawBitmapWithBorder(Bitmap bmp, int borderSize = 10)
{
int newWidth = bmp.Width + (borderSize * 2);
int newHeight = bmp.Height + (borderSize * 2);
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics gfx = Graphics.FromImage(newImage))
{
using (Brush border = new SolidBrush(Color.White))
{
gfx.FillRectangle(border, 0, 0,
newWidth, newHeight);
}
gfx.DrawImage(bmp, new Rectangle(borderSize, borderSize, bmp.Width, bmp.Height));
}
return (Bitmap)newImage;
}