LabelPlus.PicView.MakeImage C# (CSharp) Method

MakeImage() public method

public MakeImage ( Image &image, Image &imageOriginal, float zoom, List labels = null ) : bool
image Image
imageOriginal Image
zoom float
labels List
return bool
        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; }
        }

Usage Example

Ejemplo n.º 1
0
        void doOutput()
        {
            string log = "";

            Invoke(new Action(() => outPgBar.Maximum = wsp.Store.Filenames.Length));

            try
            {
                foreach (string key in wsp.Store.Filenames)
                {
                    if (checkBoxJumpNoNum.Checked && wsp.Store[key].Count == 0)
                    {
                    }
                    else
                    {
                        string outputFilename = folderBrowserDialog.SelectedPath + @"\" + key;
                        outputFilename = replaceExtension(outputFilename, radioButtonPNG.Checked ? ".png" : ".jpg");

                        string inputFilename = wsp.DirPath + @"\" + key;
                        try
                        {
                            Image in_img  = Image.FromFile(inputFilename);
                            Image out_img = null;
                            var   rslt    = pv.MakeImage(ref out_img, ref in_img, Convert.ToSingle(textBox.Text), wsp.Store[key]);
                            in_img.Dispose();
                            if (!rslt)
                            {
                                throw new FormatException();
                            }
                            else
                            {
                                using (Stream stream = new FileStream(outputFilename, FileMode.Create))
                                {
                                    out_img.Save(stream, radioButtonPNG.Checked ? ImageFormat.Png : ImageFormat.Jpeg);
                                    out_img.Dispose();
                                }
                                //stream.Close();
                            }
                        }
                        catch
                        { log += "\n" + StringResources.GetValue("can_not_output_file") + outputFilename; }
                    }
                    Invoke(new Action(() => ++ outPgBar.Value));
                }
                log += "\n\n" + StringResources.GetValue("output_complete");
            }
            catch (ThreadAbortException)
            { log += "\n\n" + StringResources.GetValue("output_aborted"); }
            catch (Exception)
            { log += "\n\n" + StringResources.GetValue("output_fail"); }
            finally
            {
                try
                {
                    MessageBox.Show(log);
                    Invoke(new Action(() => { if (button != null)
                                              {
                                                  button.Enabled = true;
                                              }
                                      }));
                    Invoke(new Action(() => { if (buttonAbort != null)
                                              {
                                                  buttonAbort.Enabled = false;
                                              }
                                      }));
                    Invoke(new Action(() => { if (outPgBar != null)
                                              {
                                                  outPgBar.Value = 0;
                                              }
                                      }));
                }
                catch { /* 窗口销毁后发生的线程冲突之 InvalidOperationException 强行不要了,嗯 */ }
            }
        }