You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于1x1像素方案,如何用SetPixel移除缩放后2x2位图的渐变?

Removing Color Gradients from a Stretched 2x2 Bitmap Using SetPixel

Alright, let's tackle this problem head-on. You’ve got a stretched version of a 2x2 bitmap (original solid colors: red, blue, green, yellow) that’s picked up color gradients during scaling, and you want to use SetPixel to strip those gradients back to clean, solid colors—just like the 1x1 bitmap solution you referenced. Here’s how to do it step by step:

Core Idea

When you scale a 2x2 solid-color bitmap, interpolation algorithms (like bilinear or bicubic) create blended gradient pixels between the original four colors. To fix this, we’ll force every pixel in the stretched image to match the original solid color of the 2x2 quadrant it belongs to.

Step-by-Step Implementation

1. Define Your Original 2x2 Color Mapping

First, lock in which original color corresponds to each quadrant of the stretched image. For a standard 2x2 grid, we’ll split the stretched image into four equal regions:

  • Top-left quadrant: Solid red (RGB(255, 0, 0))
  • Top-right quadrant: Solid blue (RGB(0, 0, 255))
  • Bottom-left quadrant: Solid green (RGB(0, 255, 0))
  • Bottom-right quadrant: Solid yellow (RGB(255, 255, 0))

2. Iterate Over Every Pixel in the Stretched Image

Loop through each (x, y) coordinate in the scaled bitmap. For each pixel, determine which quadrant it falls into, then use SetPixel to overwrite its gradient color with the corresponding solid original color.

Example Code (C# with GDI+)

Here’s a concrete example using .NET’s Bitmap.SetPixel method:

// Assume we have our stretched gradient bitmap stored here
Bitmap stretchedBitmap = new Bitmap("your_stretched_image.png");

int imgWidth = stretchedBitmap.Width;
int imgHeight = stretchedBitmap.Height;

// Calculate midpoints to split the image into 4 quadrants
int midX = imgWidth / 2;
int midY = imgHeight / 2;

// Define our original solid colors
Color solidRed = Color.FromArgb(255, 255, 0, 0);
Color solidBlue = Color.FromArgb(255, 0, 0, 255);
Color solidGreen = Color.FromArgb(255, 0, 255, 0);
Color solidYellow = Color.FromArgb(255, 255, 255, 0);

// Loop through every pixel
for (int y = 0; y < imgHeight; y++)
{
    for (int x = 0; x < imgWidth; x++)
    {
        Color targetColor;
        
        // Determine which quadrant the pixel is in
        if (y < midY)
        {
            // Top half of the image
            targetColor = x < midX ? solidRed : solidBlue;
        }
        else
        {
            // Bottom half of the image
            targetColor = x < midX ? solidGreen : solidYellow;
        }
        
        // Overwrite the gradient pixel with the solid color
        stretchedBitmap.SetPixel(x, y, targetColor);
    }
}

// Save or use the cleaned bitmap
stretchedBitmap.Save("cleaned_2x2_bitmap.png");

Key Notes

  • If your stretched image has an odd width/height, adjust the midpoint calculation (e.g., use Math.Floor(imgWidth / 2.0) to avoid off-by-one errors).
  • This approach works for any scaling factor—whether you stretched the 2x2 to 100x100 or 500x300, it’ll map every pixel back to the original solid quadrant color.
  • Just like the 1x1 solution, this completely eliminates interpolation gradients by directly enforcing the original color palette, no guesswork involved.

内容的提问来源于stack exchange,提问作者Jurica Vugrin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:02:08