C#使用图形绘制位图

C# Bitmap drawing using graphics

我正在用 C# 编写可以将随机数据绘制到位图图像的函数。 实际上它看起来可以,但它的图像看起来有渐变效果,并试图禁用它但无法成功。

我想画的是'A'但是我得到了'B'.

'A'

'B'

如何绘制像 'A' 而不是 'B' 的图像?

下面是我的代码...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BitmapTest
{
    public partial class Form1 : Form
    {
        Bitmap btImg = null;
        Graphics g = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btImg = new Bitmap(4, 4);

            g = Graphics.FromImage(btImg);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int width = 4, height = 4;

            //random number
            Random rand = new Random();

            //create random pixels
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    //generate random ARGB value
                    int a = rand.Next(256);
                    int r = rand.Next(256);
                    int g = rand.Next(256);
                    int b = rand.Next(256);

                    //set ARGB value
                    btImg.SetPixel(x, y, Color.FromArgb(a, r, g, b));
                }
            }

            Rectangle rt = new Rectangle(0, 0, width, height);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            g.DrawImageUnscaledAndClipped(btImg, rt);
            g.DrawImage(btImg, 0, 0, 4, 4);
            pictureBox1.Image = btImg;

            btImg.Save("D:\RandomImage.png");
        }
    }
}

实际上 'A' 与 'B' 相同。 'B'是表格图片,'A'是保存图片,是相同的数据。

我想你想要这个:

作为 C#,我采用了 object-oriented 的方式并创建了一个 Grid 对象来保存颜色信息,并且可以在图片框上呈现自身。

Grid.cs

public class Grid
{
    static readonly Random rng = new Random();
    readonly Color[,] data;

    public Grid(int rows, int columns)
    {
        Rows=rows;
        Columns=columns;

        data = new Color[rows, columns];

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                SetRandomColor(i, j);
            }
        }
    }

    public void SetRandomColor(int row, int column)
    {
        data[row, column] = Color.FromArgb(
            rng.Next(256),
            rng.Next(256),
            rng.Next(256),
            rng.Next(256));
    }

    public int Rows { get; }
    public int Columns { get; }

    public (int row, int column) GetCoordinates(Control target, Point point)
    {
        float wt = target.ClientSize.Width, ht = target.ClientSize.Height;
        float dx = wt/Columns, dy = ht/Rows;

        return ((int)(point.X/dx), (int)(point.Y/dy));
    }

    public Color this[int row, int column]
    {
        get => data[row, column];
        set => data[row, column]=value;
    }

    public void Render(Graphics g, Control target)
    {
        float wt = target.ClientSize.Width, ht = target.ClientSize.Height;
        float dx = wt/Columns, dy = ht/Rows;
        using (var fill = new SolidBrush(Color.Black))
        {
            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                    float x = i*dx, y = j*dy;

                    fill.Color = data[i, j];

                    g.FillRectangle(fill, x, y, dx, dy);
                }
            }
        }
    }

    public Bitmap GenerateBitmap(PictureBox target)
    {
        int wt = target.ClientSize.Width, ht = target.ClientSize.Height;
        Bitmap bmp = new Bitmap(wt, ht, PixelFormat.Format32bppArgb);
        Graphics g = Graphics.FromImage(bmp);
        Render(g, target);
        return bmp;
    }
}

接下来是在表单中定义一个网格,并与之交互。为了好玩,当您 left-click 在一个正方形上时,它会改变颜色。当您在图像上 right-click 时,它会将其保存到文件中。

Form1.cs

public partial class Form1 : Form
{
    Grid grid;
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        grid = new Grid(4, 4);

        pictureBox1.Paint += (s, ev) =>
        {
            ev.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            ev.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            grid.Render(ev.Graphics, pictureBox1);
        };
        pictureBox1.Resize += (s, ev) =>
        {
            pictureBox1.Invalidate();
        };

        pictureBox1.MouseClick += (s, ev) =>
        {
            if (ev.Button == MouseButtons.Left)
            {
                var (row, column)= grid.GetCoordinates(pictureBox1, ev.Location);
                grid.SetRandomColor(row, column);
                pictureBox1.Invalidate();
            }
            else if (ev.Button == MouseButtons.Right)
            {
                Bitmap bmp = grid.GenerateBitmap(pictureBox1);
                var fn = "D:\RandomImage.png";
                bmp.Save(fn);
                MessageBox.Show($"Image saved to {fn}.");
            }
        };
    }
}