将图像的大小调整为2x2像素,即保持颜色不变的4x4像素

本文关键字:像素 颜色 4x4 调整 2x2 图像 | 更新日期: 2023-09-27 18:22:21

请想象我有一个非常简单的棋盘图像。

在这里,我用字母作为像素表示图像:黑色像素"B",白色像素"W"

这是起始的2x2像素图像:

BW
WB

我想调整图像的大小,比如说按2倍的比例给我:

BBWW
BBWW
WWBB
WWBB

或缩放4倍

BBBBWWWW
BBBBWWWW
WWWWBBBB
WWWWBBBB

这样像素颜色就不会以任何方式抖动。

我在C#中尝试过,但图像一团糟,都是抖动的,我需要学习如何在不抖动/颜色变化的情况下做到这一点。

这是我到目前为止的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
namespace PageTwine
{
    public partial class RandonGiff : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            // create 2x2 chequerboard:
            Bitmap oBitmap = new Bitmap(2, 2);
            Graphics oGraphic = Graphics.FromImage(oBitmap);
            // color black pixels (i think the default is black but this helps to explain)
            SolidBrush oBrush = new SolidBrush(Color.FromArgb(255, 255, 255));
            oGraphic.FillRectangle(oBrush, 0, 0, 1, 1);
            oGraphic.FillRectangle(oBrush, 1, 1, 1, 1);
            //color white pixels
            oBrush = new SolidBrush(Color.FromArgb(0, 0, 0));
            oGraphic.FillRectangle(oBrush, 0, 1, 1, 1);
            oGraphic.FillRectangle(oBrush, 1, 0, 1, 1);
            // expand to 4x4
            Bitmap result = new Bitmap(4, 4);
            using (Graphics graphics = Graphics.FromImage(result))
            {
                // I don't know what these settings should be :
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                //draw the image into the target bitmap 
                graphics.DrawImage(oBitmap, 0, 0, result.Width, result.Height);
            }
            //send image directly to the browser
            Response.ContentType = "image/gif";
            result.Save(Response.OutputStream, ImageFormat.Gif);
        }
    }
}

结果是一个抖动的图像,但我需要一个干净、清晰的棋盘效果。你能帮我吗?

编辑:07/02/12

感谢到目前为止的建议,但我仍在寻找解决方案。

我创建了一个演示页面,这样你就可以自己看到结果了。

演示的URL是:

http://www.leansoftware.net/servicedesk/publicadhoc/randomgif.aspx?columns=3&行=3

该演示将创建一个以初始列x行为像素的棋盘,然后放大为300x300px的gif图像。

你会发现颜色失真了——我正试图解决这个问题。

以下是演示的源代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;

    public partial class RandomGif : System.Web.UI.Page
    {
        public class Coordinates
        {
            public int x { get; set; }
            public int y { get; set; }
        }
        static Random rand = new Random();
        protected void Page_Load(object sender, EventArgs e)
        {
            // Output type gif
            int iwidth;
            int iheight;
            int bits;
            Response.ContentType = "image/gif";
            // Get parameters
            if (Request.QueryString["count"] != null)
            {
                bits = Convert.ToInt32(Request.QueryString["count"]);
                int square = Convert.ToInt32(Math.Ceiling(Math.Sqrt(bits + Math.Floor(bits / 4.0))));
                iwidth = square;
                iheight = square;
                bits = (square * square)-1;
            }
            else
                if (Request.QueryString["rows"] != null && Request.QueryString["columns"] != null)
                {
                    iwidth = Convert.ToInt32(Request.QueryString["columns"]);
                    iheight = Convert.ToInt32(Request.QueryString["rows"]);
                    bits = (iwidth * iheight);
                }
                else {
                    return;
                }
        if (bits > 1000){
        Response.Write("Please specify a grid <= 1000 pixels");
        return;
        }
            int plotCount = 0;
            //web safe colors : 00, 33, 66, 99, CC, FF;
            List<int> webSafeColor = new List<int>();
            webSafeColor.Add(0); //00
            webSafeColor.Add(51);//33
            webSafeColor.Add(102);//66
            webSafeColor.Add(153);//99
            webSafeColor.Add(204);//CC
            webSafeColor.Add(255);//FF
            // Create a structure to hold all possible coordinates
            var Grid = new List<Coordinates>();
            for (int xi = 1; xi <= iwidth; xi++)
            {
                for (int yi = 1; yi <= iheight; yi++)
                {
                    Grid.Add(new Coordinates { x = xi, y = yi });
                    plotCount++;
                }
            }
            //create a new Bitmap object
            Bitmap oBitmap = new Bitmap(iwidth, iheight);
            //create a new Graphics object, which will allow us to draw on our bitmap:
            Graphics oGraphic = Graphics.FromImage(oBitmap);

            //fill the image rectangle with n bits
            for (int i = 1; i <= bits; i++)
            {
                //Random rand = new Random();
                int row = rand.Next(Grid.Count());
                // random red
                int ircolor = webSafeColor[rand.Next(5)];
                // random green
                int igcolor = webSafeColor[rand.Next(5)];
                // random blue
                int ibcolor = webSafeColor[rand.Next(5)];
                Color randomColor = Color.FromArgb(ircolor, igcolor, ibcolor);
                SolidBrush oBrush = new SolidBrush(randomColor);
                oGraphic.FillRectangle(oBrush, Grid[row].x - 1, Grid[row].y - 1, 1, 1);
        // Delete this coordinate#
                Grid.Remove(Grid[row]);
            }
            // resize image
            Bitmap result = new Bitmap(300, 300);
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality 
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.AssumeLinear;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half; 
                //draw the image into the target bitmap 
                graphics.DrawImage(oBitmap, 0, 0, result.Width, result.Height);
            }
            //send image directly to the browser
            Response.ContentType = "image/gif";
            result.Save(Response.OutputStream, ImageFormat.Gif);
        }
    }

如果你能提出修改建议,我们可以尝试一下,看看它是否能解决问题。

感谢Richard

将图像的大小调整为2x2像素,即保持颜色不变的4x4像素

这可能是c#Draw Image(Scaled)to Graphics的重复,无法正确插值。修复?

您需要添加:

graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;

或者,您可以使用

graphics.DrawImage(oBitmap, 0, 0, result.Width * 2, result.Height * 2);

以实现迫使画笔填充而不在右下图像边缘处翘曲的类似效果。

更新:

添加使用自定义调色板创建索引图像的链接。http://support.microsoft.com/kb/319061

使用:

System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;