【问题标题】:Remove color gradient from scaled unit bitmap从缩放的单位位图中删除颜色渐变
【发布时间】:2014-07-31 07:54:04
【问题描述】:

我正在尝试通过ys 红色Bitmap 创建xs 并且遇到问题。

我的功能如下:(参考: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,它根本没有提到添加渐变,我什至不确定为什么这是默认行为。

这是一个可以合作的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;
    }
}

【问题讨论】:

  • 为什么不直接创建一个new bitmap(xs, ys) 并将其涂成红色?
  • 使用.SetPixel(...) 功能?我正在这样做,但速度很慢。

标签: c# image winforms bitmap


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多