0

I have captured a JPG image using a Webcam in C# and have stored it in a folder.

Now I want to convert it to an 8-bit PNG image.

I checked all over internet and Stack Overflow, but none of the proposed solutions work for me.

Here is what I am currently using:

Bitmap img = new Bitmap(imgPath);
Bitmap img8 = new Bitmap(imgW, imgH, PixelFormat.Format16bppRgb565);

for (int I = 0; I <= img.Width - 1; I++)
    for (int J = 0; J <= img.Height - 1; J++) 
        img8.SetPixel(I, J, img.GetPixel(I, J));

However, this throws the following exception:

SetPixel is not allowed for indexed pixel images.

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
Raja Sajid
  • 35
  • 1
  • 3
  • 10
  • 4
    Is there a reason you can't use `img.Save(@"c:\somefile.png", System.Drawing.Imaging.ImageFormat.Png)`? – Steven Hansen Feb 10 '14 at 15:52
  • 1
    First, that's a very inefficient way to convert images - Steven's way is much better. Also, converting to 8-bit PNG image is even more problematic, since you have to build a palette and encode the data using that. If you really want an 8-bit PNG, you should use a conversion library that supports this, and note that this may result in data loss (when converting from JPG, that's pretty much a given). You can use GDI+ for that, for example http://www.wischik.com/lu/programmer/1bpp.html or Antoine's answer. – Luaan Feb 10 '14 at 16:02
  • Sajid, Are you OK with GIF OR TIFF? – L.B Feb 10 '14 at 16:55
  • Hello, No i cant use Gif. BUt can use TIFF. – Raja Sajid Feb 10 '14 at 17:07
  • @Steven Hansen i dont want to save image as is. I want this in pixel depth of 8bit. – Raja Sajid Feb 10 '14 at 17:07
  • Do you mean you want an image that has a source palette? Determining which palette should be used for any image is not necessarily trivial, and is not always automatic. You will definitely need a third-party library to perform the conversion. – Steven Hansen Feb 10 '14 at 18:11
  • Don't use 8 bit image format if you don't know what 8 bit image format really _is_. It's not some magical way of making images smaller. It's not an automatic quality reduction by using less bits, like 16bpp. It's a process of selecting the 256 most prominent colours from your image, which is not a trivial operation, and you'll need code that actually does that. – Nyerguds Jan 09 '18 at 09:34

3 Answers3

2

You could try to use ImageMagick, it contains a utility to convert files. You can use either the command line interface or a .Net connector like magick.Net.

Using the command line, you could launch a process that runs the following command:

convert [yourfile.jpg] -depth 8 [yourfile.png]

That's probably overkill, but that's the only option I know to work on images.

Antoine
  • 5,055
  • 11
  • 54
  • 82
  • Hello i need this for Asp.net .. Will it work in Asp.net Web Application. – Raja Sajid Feb 10 '14 at 17:06
  • You'll need to install imagemagick on your server, but there's nothing preventing this solution from working in ASP.Net. – Antoine Feb 11 '14 at 10:27
  • 1
    You don't need to install ImageMagick. You only need to install Visual C++ Redistributable (https://magick.codeplex.com/documentation) – dlemstra Feb 14 '14 at 13:28
2

No i cant use Gif. BUt can use TIFF.

Image orgBmp = Image.FromFile(@"fname.jpg");

var tiffEncoder = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
                  .First(e => e.FormatDescription == "TIFF");


EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth, (long)ColorDepth.Depth8Bit);

orgBmp.Save(@"fname.tif", tiffEncoder, parameters);
L.B
  • 114,136
  • 19
  • 178
  • 224
0

you have to perform some programming by your self. you can find an example logic and code here http://codingresolved.com/discussion/2185/save-png-image-as-8bit-format-in-c

Waqas
  • 31
  • 1
  • 1
    Link is broken (which is why you should _post_ code, not _link_ it), and it specifically converts to grayscale, which is not what the question asked. – Nyerguds Jan 09 '18 at 09:33