2014-06-10 43 views
1

我正在尝试创建xsys红色Bitmap并且出现问题。从缩放的单元位图中删除颜色渐变

我的功能,这样做看起来是这样的:(参考: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); 
} 

虽然问题是,这造成整个Bitmap颜色渐变,我只是想将颜色指定的每个像素。我如何去除这个渐变和“完全”的颜色Bitmap

这里是我使用的构造函数http://msdn.microsoft.com/en-us/library/334ey5b7.aspx它没有提到添加渐变,我什至不知道为什么这将是默认行为。

enter image description here

这里是一个SSCCE与

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; 
    } 
} 
+0

为什么不创建一个'新的位图(xs,ys)',只是把它涂成红色? – LarsTech

+0

使用'.SetPixel(...)'函数?我正在那样做,但速度很慢。 – AnotherUser

回答

1

工作只是创建一个你想要的大小一个新的位图,写生,红色:

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; 
} 
+0

这工作非常好!谢谢! – AnotherUser