Below I have posted the code I use for drawing pictures. The problem is that when I want to generate more images in full hd size (in loop), the generation time increases significantly. Can somehow optimize my code to speed it up a lot?
Bitmap bmp = new Bitmap(imageMask.Width, imageMask.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
bmp.MakeTransparent();
Bitmap bmpLevel = new Bitmap(imageMask.Width, imageMask.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics g = Graphics.FromImage(bmpLevel))
{
int rectX = levelX.Value;
int rectY = levelY.Value + levelHeight.Value - valproc;
int rectWidth = levelWidth.Value;
int rectHeight = valproc;
g.FillRectangle(new SolidBrush(levelColor), rectX, rectY, rectWidth, rectHeight);
g.DrawImage(imageMask, new Rectangle(0, 0, bmpLevel.Width, bmpLevel.Height), new Rectangle(0, 0, imageMask.Width, imageMask.Height), GraphicsUnit.Pixel);
}
bmpLevel.MakeTransparent(Color.Black);
Bitmap contour = new Bitmap(imageMaskCountour.Width, imageMaskCountour.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
contour.MakeTransparent();
using (Graphics g = Graphics.FromImage(contour))
{
g.FillRectangle(ContourBrush, 0, 0, contour.Width, contour.Height);
g.DrawImage(imageMaskCountour, new Rectangle(0, 0, contour.Width, contour.Height), new Rectangle(0, 0, imageMaskCountour.Width, imageMaskCountour.Height), GraphicsUnit.Pixel);
}
contour.MakeTransparent(Color.Black);
bmp.MakeTransparent(Color.Black);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawImage(contour, new Rectangle(0, 0, bmp.Width, bmp.Height), new Rectangle(0, 0, contour.Width, contour.Height), GraphicsUnit.Pixel);
g.DrawImage(bmpLevel, new Rectangle(0, 0, bmp.Width, bmp.Height), new Rectangle(0, 0, bmpLevel.Width, bmpLevel.Height), GraphicsUnit.Pixel);
}
if (this.HiHiLevel.HasValue)
{
using (Graphics g = Graphics.FromImage(bmp))
{
int alarmlevel = (int)(((double)this.HiHiLevel.Value / 100.0) * (double)levelHeight);
int x = levelX.Value;
int y = levelY.Value + levelHeight.Value - alarmlevel;
Pen drawPen = new Pen(Color.Red, 2);
g.DrawLine(drawPen, new Point(x, y + 5), new Point(x + levelWidth.Value, y + 5));
}
}
if (this.HiLevel.HasValue)
{
using (Graphics g = Graphics.FromImage(bmp))
{
int alarmlevel = (int)(((double)this.HiLevel.Value / 100.0) * (double)levelHeight);
int x = levelX.Value;
int y = levelY.Value + levelHeight.Value - alarmlevel;
Pen drawPen = new Pen(Color.Yellow, 2);
g.DrawLine(drawPen, new Point(x, y + 5), new Point(x + levelWidth.Value, y + 5));
}
}
if (this.LoLevel.HasValue)
{
using (Graphics g = Graphics.FromImage(bmp))
{
int alarmlevel = (int)(((double)this.LoLevel.Value / 100.0) * (double)levelHeight);
int x = levelX.Value;
int y = levelY.Value + levelHeight.Value - alarmlevel;
Pen drawPen = new Pen(Color.Yellow, 2);
g.DrawLine(drawPen, new Point(x, y + 5), new Point(x + levelWidth.Value, y + 5));
}
}
if (this.LoLoLevel.HasValue)
{
using (Graphics g = Graphics.FromImage(bmp))
{
int alarmlevel = (int)(((double)this.LoLoLevel.Value / 100.0) * (double)levelHeight);
int x = levelX.Value;
int y = levelY.Value + levelHeight.Value - alarmlevel;
Pen drawPen = new Pen(Color.Red, 2);
g.DrawLine(drawPen, new Point(x, y + 5), new Point(x + levelWidth.Value, y + 5));
}
}
using (System.IO.MemoryStream msItem = new System.IO.MemoryStream())
{
bmp.Save(msItem, System.Drawing.Imaging.ImageFormat.Png);
Schema = msItem.ToArray();
}
Graphics
andBitmap
s instead of reusing one? Note thatMakeTransparent
will convert an image to 32 bpp ARGB, so you may as well specify that format when creating your bitmaps (and remove any redundantMakeTransparent
calls). Also,Pen
s must be disposed. Either way, the code you've shown here is not complete, which makes it difficult to give proper advice. You may want to update your post. \$\endgroup\$