将属性添加到通过调用构造函数中的另一个方法创建的位图对象

时间:2014-01-17 14:56:34

标签: c# image graphics bitmap

我有一个接收bitmap对象的方法,并在其上覆盖日期和时间字符串并返回新的bitmap。代码如下。

    public static Bitmap overlayBitmap(Bitmap sourceBMP, int width, int height, List<String> times, List<String> dates, IEnumerable<Color> colors) {

        // Determine the new width
        float newWidth = width + (width / 3.0f);
        float newHeight = height + (height / 3.0f);

        // Intelligent vertical + horizontal text distance calculator
        float verticalDistance = height / (times.Count - 1.0f);
        float horizontalDistance = width / (dates.Count - 1.0f);

        Bitmap result = new Bitmap((int)newWidth, (int)newHeight);
        using (Graphics g = Graphics.FromImage(result)) {

            // Background color
            Brush brush = new SolidBrush(colors.First());
            g.FillRectangle(brush, 0, 0, newWidth, newHeight);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

            // Times text configs
            StringFormat stringFormatTimes = new StringFormat();
            stringFormatTimes.LineAlignment = StringAlignment.Center;
            stringFormatTimes.Alignment = StringAlignment.Center;
            Font drawFontY = new Font("Whitney", newHeight / 70);

            // Dates text configs
            StringFormat stringFormatDates = new StringFormat();
            stringFormatDates.LineAlignment = StringAlignment.Center;
            stringFormatTimes.Alignment = StringAlignment.Center;
            stringFormatDates.FormatFlags = StringFormatFlags.DirectionVertical;
            Font drawFontX = new Font("Whitney", newHeight / 70);

            // Location of times text
            for (int i = 0; i < times.Count; i++) {
                if (i % determineIncrementTimes(times.Count) == 0) {
                    g.DrawString(times[i], drawFontX, Brushes.White, (((newWidth - width) / 2) / 2), ((newHeight - height) / 2) + (verticalDistance * i), stringFormatTimes);
                }
            }

            // Location of dates text
            for (int i = 0; i < dates.Count; i++) {
                if (i % determineIncrementDates(dates.Count) == 0) {
                    g.DrawString(dates[i], drawFontY, Brushes.White, ((newWidth - width) / 2) + (horizontalDistance * i), ((newHeight - height) / 2) + height, stringFormatDates);
                }
            }

            // New X and Y Position of the sourceBMP within the new BMP.
            int XPos = width / 6;
            int YPos = height / 6;

            // Int -> Float casting for the outline
            float fXPos = width / 6.0f;
            float fYPos = height / 6.0f;

            float fWidth = width / 1.0f;
            float fHeight = height / 1.0f;

            // Draw new image at the position width/6 and height/6 with the size at width and height
            g.DrawImage(sourceBMP, fXPos, fYPos, fWidth, fHeight);
            g.DrawRectangle(Pens.White, fXPos, fYPos, fWidth, fHeight); // white outline
            g.Dispose();
        }
        return result;
    }

我担心的是,对于下一个开发人员,我希望能够轻松访问和设置当前我只是“硬编码”的特定值。例如,通过计算的时间文本的x位置这段代码:

(((newWidth - width) / 2) / 2)

实际上,我希望开发人员只需输入以下内容即可访问和/或设置此值:

something.XPos = [someFloat];

如何使用上面的方法(伪代码)如下:

private readonly Bitmap _image;
private readonly Bitmap _overlayedImage;

public myConstructor(int someInputValues){
    // some code that generates the first bitmap called _image
    _newImage = overlayImage(_image, ....);
}

作为参考,这是绘制的图像:

enter image description here

我的问题是 - 由于某些值需要首先进行转换和初始化,我可以在方法结束时,在结束括号之前设置我的实例变量吗?

1 个答案:

答案 0 :(得分:0)

public Bitmap overlayBitmap
{
    get
    {
        // Build bitmap overlay
        return overlayBitmapOutput;
    }
...
}

[编辑:答案不足&gt;&gt;等待]