如何在内存使用.NET动态创建一个JPG图片? [英] How to create a jpg image dynamically in memory with .NET?

查看:207
本文介绍了如何在内存使用.NET动态创建一个JPG图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET(3.5 SP1)库(DLL)在C#编写。我有一类方法,将具有以下签名来扩展这个库:

I have a .NET (3.5 SP1) library (DLL) written in C#. I have to extend this library by a class method which will have the following signature:

public byte[] CreateGridImage(int maxXCells, int maxYCells,
    int cellXPosition, int cellYPosition)
{
    ...
}

此方法应该做到以下几点:

This method is supposed to do the following:

  • 输入参数 maxXCells maxYCells 定义单元格的X和Y方向的网格的大小。 maxXCells <$ C C $>和 maxYCells 细胞是在每个方向的数目。各个单元方形。 (所以这是一种不对称的国际象棋棋盘的。)
  • 输入参数 cellXPosition cellYPosition 确定该网格内的一个特殊的小区和该小区已被填充交叉。
  • 没有花哨的图形是必要的,在白色背景上与中的X细胞的人真的只有黑色网格线。
  • 在生成的图形必须有jpg格式。
  • 在创建这个图形必须发生在内存中,并没有什么应该被存储在磁盘上的文件,也没有在屏幕上画。
  • 在该方法返回生成的图像作为字节[]
  • Input parameters maxXCells and maxYCells define the size of a grid of cells in X and Y direction. maxXCells and maxYCells is the number of cells in each direction. The individual cells are square-shaped. (So it's kind of an asymmetric chess board.)
  • Input parameters cellXPosition and cellYPosition identify one special cell within this grid and this cell has to be filled with a cross.
  • No fancy graphics are necessary, really only black grid lines on a white background and a X in one of the cells.
  • The resulting graphic must have jpg format.
  • Creation of this graphic must happen in memory and nothing should be stored in a file on disk nor painted on the screen.
  • The method returns the generated image as a byte[]

我很熟悉.NET中的图形功能,所以我的问题是:

I'm very unfamiliar with graphics functions in .NET so my questions are:

  • 这是可能在所有与.NET 3.5 SP1,无需额外的第三方库(我想避免)?
  • 有哪些基本步骤,我必须遵循,什么是重要的.NET命名空间,类和方法,我需要知道实现这个目标(epecially画内存线等简单的图形元素,并转换结果成JPG格式的字节数组)?

感谢您的建议,提前!

推荐答案

以下是将使用GDI绘制网格和放置一个十字(红色背景),就像在这个例子中一个完整的code样品下面的图片。它采用GDI就像其他的答案,但在实际工作中,通过细胞循环和绘图网格线需要的地方。

The following is a full code sample that will use GDI to draw a grid and place a cross (with a red background) just like in the example image below. It uses GDI just like the other answers but the real work takes places by looping through the cells and drawing gridlines.

下面code

byte[] bytes = CreateGridImage(10,10, 9, 9, 30);

将创建一个10×10网格在9x9的位置交叉:

will create a 10x10 grid with a cross in the 9x9 position:

一个新增加的CreateGridImage是增加了一个boxSize论证它设置在网格的每个广场大小

A new addition to the CreateGridImage is the addition of a boxSize argument which sets the size of each "square" in the grid

public static byte[] CreateGridImage(
        int maxXCells,
        int maxYCells,
        int cellXPosition,
        int cellYPosition,
        int boxSize)
        {
             using (var bmp = new System.Drawing.Bitmap(maxXCells * boxSize+1, maxYCells * boxSize+1))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.Clear(Color.Yellow);
                    Pen pen = new Pen(Color.Black);
                    pen.Width = 1;

                    //Draw red rectangle to go behind cross
                    Rectangle rect = new Rectangle(boxSize * (cellXPosition - 1), boxSize * (cellYPosition - 1), boxSize, boxSize);
                    g.FillRectangle(new SolidBrush(Color.Red), rect);

                    //Draw cross
                    g.DrawLine(pen, boxSize * (cellXPosition - 1), boxSize * (cellYPosition - 1), boxSize * cellXPosition, boxSize * cellYPosition);
                    g.DrawLine(pen, boxSize * (cellXPosition - 1), boxSize * cellYPosition, boxSize * cellXPosition, boxSize * (cellYPosition - 1));

                    //Draw horizontal lines
                    for (int i = 0; i <= maxXCells;i++ )
                    {
                        g.DrawLine(pen, (i * boxSize), 0, i * boxSize, boxSize * maxYCells);
                    }

                    //Draw vertical lines            
                    for (int i = 0; i <= maxYCells; i++)
                    {
                        g.DrawLine(pen, 0, (i * boxSize), boxSize * maxXCells, i * boxSize);
                    }                    
                }

                var memStream = new MemoryStream();
                bmp.Save(memStream, ImageFormat.Jpeg);
                return memStream.ToArray();
            }
        }

这篇关于如何在内存使用.NET动态创建一个JPG图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆