为了将多次调用方法画在同一个图片上,全局定义了图片和画图板
static Bitmap result = new Bitmap(1000, 1000);//新建一个bmp图片
Graphics g = Graphics.FromImage(result);
表格及字符分别在不同方法体内实现,画在同一个图片内
private void WriteTableToPicture(string strCode,int w, int h)
{
char[] chars1 = strCode.ToCharArray();
Bitmap tp1 = new Bitmap(w, h);
if (chars1.Length == w/2 * h)
{
for (int i = 0; i < w/2; i++)
for (int j = 0; j < h; j++)
{
if (chars1[(i * h + j)].ToString() == "1")
tp1.SetPixel(i, j, Color.Black);
else
tp1.SetPixel(i, j, Color.White);
}
}
g.DrawImage(tp1, new Rectangle(currentCol, currentLine, w, h));
}
private void writeStringToPicture(string buffer)
{
Font font = new Font("宋体", 10f);
g.DrawString(buffer, font, Brushes.Black, new Point(currentCol, currentLine));
font.Dispose();
}
但要生成的图片大小并不是开始确定的,而是根据接收到的数据的多少,所以static Bitmap result = new Bitmap(1000, 1000);高度定义为1000并不合适,有可能要比这大的多,比如10000,所以我想根据实际大小去定义图片大小,但是result还必须定义为全局的,无法根据数据量多少改变大小。请问有什么方法可以根据数据量的大小来动态生成result的高度吗?
static Bitmap result = new Bitmap(1000, 1000);//新建一个bmp图片
Graphics g = Graphics.FromImage(result);
表格及字符分别在不同方法体内实现,画在同一个图片内
private void WriteTableToPicture(string strCode,int w, int h)
{
char[] chars1 = strCode.ToCharArray();
Bitmap tp1 = new Bitmap(w, h);
if (chars1.Length == w/2 * h)
{
for (int i = 0; i < w/2; i++)
for (int j = 0; j < h; j++)
{
if (chars1[(i * h + j)].ToString() == "1")
tp1.SetPixel(i, j, Color.Black);
else
tp1.SetPixel(i, j, Color.White);
}
}
g.DrawImage(tp1, new Rectangle(currentCol, currentLine, w, h));
}
private void writeStringToPicture(string buffer)
{
Font font = new Font("宋体", 10f);
g.DrawString(buffer, font, Brushes.Black, new Point(currentCol, currentLine));
font.Dispose();
}
但要生成的图片大小并不是开始确定的,而是根据接收到的数据的多少,所以static Bitmap result = new Bitmap(1000, 1000);高度定义为1000并不合适,有可能要比这大的多,比如10000,所以我想根据实际大小去定义图片大小,但是result还必须定义为全局的,无法根据数据量多少改变大小。请问有什么方法可以根据数据量的大小来动态生成result的高度吗?
改为static Bitmap result =null
在知道后在初始化,可以试试
Bitmap result; //新建一个bmp图片//修改下面的函数:
private void WriteTableToPicture(string strCode,int w, int h)
{
char[] chars1 = strCode.ToCharArray();
result = new Bitmap(w, h);
if (chars1.Length == w/2 * h)
{
for (int i = 0; i < w/2; i++)
for (int j = 0; j < h; j++)
{
if (chars1[(i * h + j)].ToString() == "1")
result.SetPixel(i, j, Color.Black);
else
result.SetPixel(i, j, Color.White);
}
}
}// 修改下面的函数
private void writeStringToPicture(string buffer)
{
Font font = new Font("宋体", 10f);
Graphics g = Graphics.FromImage(result);
g.DrawString(buffer, font, Brushes.Black, new Point(currentCol, currentLine));
font.Dispose();
}