1

I am trying to create a xs by ys red Bitmap and am having issues.

My function to do so looks like this: (Ref: https://stackoverflow.com/a/12502878/1596244)

private Bitmap getBlankBitmap(int xs, int ys)
{
    Bitmap b = new Bitmap(1, 1);
    b.SetPixel(0, 0, Color.Red);
    return new Bitmap(b, xs, ys);
}

Though the issue is, this creates a color gradient across the Bitmap, I just want each pixel to be the color specified. How can I remove this gradient and "fully" color the Bitmap?

Here is the constructor I am using http://msdn.microsoft.com/en-us/library/334ey5b7.aspx which doesn't mention adding a gradient at all, I'm not even sure why that would be the default behavior.

enter image description here

Here is an SSCCE to work with

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

namespace TestProject2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            showIssue();
        }

        void showIssue()
        {
            pictureBox1.Image = getBlankBitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
        }

        private Bitmap getBlankBitmap(int xs, int ys)
        {
            Bitmap b = new Bitmap(1, 1);
            b.SetPixel(0, 0, Color.Red);
            return new Bitmap(b, xs, ys);
        }

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(477, 344);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(477, 344);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        private System.Windows.Forms.PictureBox pictureBox1;
    }
}
Community
  • 1
  • 1
AnotherUser
  • 1,311
  • 1
  • 14
  • 35

1 Answers1

1

Just create a new bitmap with the size you want and paint it red:

private Bitmap getBlankBitmap(int width, int height) {
  Bitmap b = new Bitmap(width, height);
  using (Graphics g = Graphics.FromImage(b)) {
    g.Clear(Color.Red);
  }
  return b;
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225