简体   繁体   中英

.net is saving my programmatically created bitmap incorrectly

I have some code that creates a 1*1000 px gradient and saves it as a bmp.

static void Main(string[] args)
{
    int height = 1000;
    int value1 = 249;
    int value2 = 227;

    string folder = "C:\\Users\\TehSuckerer\\Desktop\\Gradient\\";

    Bitmap bitmap = new Bitmap(1, height);

    for (int i = 0; i < height; ++i)
    {
        int value = Dither1D(Lerp(value1, value2, (float)i / height), i);
        bitmap.SetPixel(0, i, Color.FromArgb(value, value, value));
    }

    for (int i = 0; i < height; ++i)
    {
        Debug.WriteLine(bitmap.GetPixel(0, i).ToString());
    }

    if (!Directory.Exists(folder))
        Directory.CreateDirectory(folder);

    try
    {
        bitmap.Save(folder + "Gradient.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
    }
    catch (System.IO.IOException e)
    {
        Console.WriteLine(e.Message);
    }
}

static float Lerp (int value1, int value2, float lerp)
{
    return (1 - lerp) * value1 + lerp * value2;
}

static float SineEaseOut (int value1, int value2, float lerp)
{
    return Lerp(value1, value2, (float)Math.Sin(lerp * Math.PI / 2));
}

static int Dither1D (float value, int coord)
{
    float remainder = value % 1;
    if (remainder < 0.25 || (remainder < 0.75 && (coord % 2 == 0)))
        return (int)Math.Floor(value);
    return (int)Math.Ceiling(value);
}

As you can see, I write all the color values into the log just before I save the bmp, to be sure.
They look good, the last 20 values are

(...)  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=228, G=228, B=228]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=228, G=228, B=228]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=228, G=228, B=228]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=228, G=228, B=228]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]  
Color [A=255, R=227, G=227, B=227]

In other words, there is some part that is dithered between 227 and 228, which is what I want. However, when I save the file and open it in Photoshop, it looks nothing like that. Here, the last 20 values are

(...)  
228  
228  
228  
228  
227  
228  
228  
228  
227  
227  
227  
227  
227  
227  
227  
227  
227  
227  
227  
227

I just don't understand what is happening here. How can I make .net save the image correctly?

(Posted on behalf of the OP) .

It seems I misunderstood the situation. The file is saved correctly. The error happened when I stretched the image horizontally in Photoshop to make it easier to see. I need to select "nearest neighbor" or the pixels will get corrupted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM