I have 100's of pictures who arent using a true "white" as the background and am creating a simple app to replace anything I consider "whitish" to pure white.
The app works fine for all but a few bitmaps that were in a different format (they are using a color palette).
I thought the code below would work fine, but it isnt. It searches for the 254,254,254 (fefefe) color, and replaces it with 255,255,255 (ffffff).
I think the code will explain best whats going on...
...if you follow through the code (or use WriteLine to dump out the color info and watch it) the test enters the if in the section that isnt working correctly (if pal.Entries....).
Then it correctly changes the palette value in the index (in the case of the bitmap I am using, its the last index in the palette). It even correctly assigns the new palette to the bitmap's palette, and the bitmap shows the new correct color in its last index.
The problem is, when you open the bitmap (I am using Paint Shop Pro), the index isnt actually changed. Additionally, if you run the same bitmap through the app again, it functions as if it were the first time (and nothing was changed from the previous run).
I am at a loss as to why this is occuring, and would very much appreciate any insight offered.
Tj
The app works fine for all but a few bitmaps that were in a different format (they are using a color palette).
I thought the code below would work fine, but it isnt. It searches for the 254,254,254 (fefefe) color, and replaces it with 255,255,255 (ffffff).
I think the code will explain best whats going on...
Code:
---------------------------------------------------------------------------------------
Bitmap mybitmap = new Bitmap(srcFilePath);
if(mybitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format4bppIndexed) {
// working code
}
else {
// this section not functioning correctly
System.Drawing.Imaging.ColorPalette pal = mybitmap.Palette;
for (int i = 0; i < pal.Entries.Length; i++) {
if (pal.Entries[i].Name == "fffefefe") {
pal.Entries[i] = Color.FromArgb(255, 255, 255);
}
}
mybitmap.Palette = pal;
}
mybitmap.Save(destFilePath);
...if you follow through the code (or use WriteLine to dump out the color info and watch it) the test enters the if in the section that isnt working correctly (if pal.Entries....).
Then it correctly changes the palette value in the index (in the case of the bitmap I am using, its the last index in the palette). It even correctly assigns the new palette to the bitmap's palette, and the bitmap shows the new correct color in its last index.
The problem is, when you open the bitmap (I am using Paint Shop Pro), the index isnt actually changed. Additionally, if you run the same bitmap through the app again, it functions as if it were the first time (and nothing was changed from the previous run).
I am at a loss as to why this is occuring, and would very much appreciate any insight offered.
Tj