public bool MakeImage(ref Image image,ref Image imageOriginal, float zoom = 0, List<LabelItem> labels = null)
{
try
{
if (zoom == 0) zoom = this.Zoom;
if (labels == null) labels = this.labels;
//若无东西 就清空image
if (imageOriginal == null)
{
if (image != null)
{
image.Dispose();
image = null;
}
return false;
}
//图像来自外部 清除内部缓存
if (imageOriginal != this.imageOriginal) {
if (imageZoomed != null) {
imageZoomed.Dispose();
imageZoomed = null;
}
}
//判断有没有必要生成
if (imageZoomed != null && Math.Abs(this.Zoom - zoom) < 0.001f && !EnableMakeImage)
return false;
//缩图
if ( (imageZoomed == null) ||
!(Math.Abs(imageZoomedZoomValue - zoom)<0.001))
{
imageZoomedZoomValue = zoom;
if (imageZoomed != null)
imageZoomed.Dispose();
imageZoomed = new Bitmap(imageOriginal, (int)(imageOriginal.Size.Width * zoom), (int)(imageOriginal.Size.Height * zoom));
}
//释放缓存
if (image != null)
image.Dispose();
//贴上标签
image = new Bitmap(imageZoomed);
Graphics tmp = Graphics.FromImage(image);
if (!hideLabel && labels!=null)
{
float labelFontSize = LabelSideLength(image) / 1.6f;
for (int i = 0; i < labels.Count; i++)
{
RectangleF rect = getLabelRectangle(labels[i].X_percent, labels[i].Y_percent, image);
Font myFont = new System.Drawing.Font(new FontFamily("Arial"), labelFontSize, FontStyle.Bold);
Brush myBrushRed = new SolidBrush(colorList[labels[i].Category - 1]);
Brush myBrushWhite = new SolidBrush(Color.White);
Pen mySidePen = new Pen(myBrushRed, LabelSideLength(image) / 10f);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
//实体字
tmp.DrawString((i + 1).ToString(), myFont, myBrushRed, rect, sf);
//外框
//tmp.DrawRectangle(mySidePen, rect.X, rect.Y, rect.Width, rect.Height);
//显示Group
if (AlwaysShowGroup || showGroup)
{
Font groupFont = new System.Drawing.Font(new FontFamily("simsun"), labelFontSize / 1.5f, FontStyle.Bold);
float myWidth = labelFontSize * 10;
RectangleF groupRect = new RectangleF(
rect.X + rect.Width / 2 - myWidth / 2,
rect.Y - labelFontSize,
myWidth,
rect.Height);
tmp.DrawString(groupString[labels[i].Category - 1], groupFont, myBrushRed, groupRect, sf);
groupFont.Dispose();
}
myFont.Dispose();
myBrushRed.Dispose();
myBrushWhite.Dispose();
mySidePen.Dispose();
sf.Dispose();
}
}
Refresh();
tmp.Dispose();
return true;
}
catch { return false; }
}