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

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

问题描述

我有一个用 C# 编写的 .NET (3.5 SP1) 库 (DLL).我必须通过一个具有以下签名的类方法来扩展这个库:

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:

  • 输入参数 maxXCellsmaxYCells 定义了 X 和 Y 方向的单元格网格的大小.maxXCellsmaxYCells 是每个方向的单元格数.单个细胞呈方形.(所以这是一个不对称的棋盘.)
  • 输入参数 cellXPositioncellYPosition 标识此网格中的一个特殊单元格,该单元格必须用叉号填充.
  • 不需要花哨的图形,实际上只有白色背景上的黑色网格线和一个单元格中的 X.
  • 生成的图形必须为 jpg 格式.
  • 此图形的创建必须在内存中进行,不应将任何内容存储在磁盘上的文件中,也不应在屏幕上绘制.
  • 该方法将生成的图像作为 byte[]
  • 返回
  • 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 命名空间、类和方法(尤其是在内存中"绘制线条和其他简单图形元素并转换结果)转换成 jpg 格式的字节数组)?

提前感谢您的建议!

推荐答案

以下是一个完整的代码示例,它将使用 GDI 绘制一个网格并放置一个十字(红色背景),就像下面的示例图像一样.它像其他答案一样使用 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.

以下代码

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

将创建一个 10x10 的网格,在 9x9 的位置有一个十字:

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

CreateGridImage() 的新增功能是添加一个 boxSize 参数,该参数设置网格中每个正方形"的大小

A new addition to 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天全站免登陆