从缩放的单位位图中删除颜色渐变 [英] Remove color gradient from scaled unit bitmap
问题描述
我正在尝试按 ys
红色位图$ c创建
xs
$ c>我遇到了问题。
I am trying to create a xs
by ys
red Bitmap
and am having issues.
我这样做的功能如下:(参考: https:/ /stackoverflow.com/a/12502878/1596244 )
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);
}
虽然问题是,这会在<$ c $上创建一个颜色渐变c>位图,我只希望每个像素都是指定的颜色。如何删除此渐变并完全着色位图
?
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
?
这是我使用的构造函数 http://msdn.microsoft.com/en-us/library/334ey5b7.aspx 没有提到添加渐变,我甚至不确定为什么这将是默认行为。
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.
这是一个使用System的SSCCE
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;
}
}
推荐答案
创建一个你想要的大小的新位图并将其绘制成红色:
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;
}
这篇关于从缩放的单位位图中删除颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!