Articles in this section
Category / Section

How to convert color image to black and white/gray image in WinForms PDF?

1 min read

We can change color image to black and white/gray image in an existing WinForms PDF with the help of ExtractImages and ReplaceImage features and find the code snippet and sample below.

Code snippet for Converting Color image to black and white\gray image:

public Bitmap ConvertToBlackandWhite(Bitmap image)
{
 Bitmap bitmap = new Bitmap(image);
 for (int i = 0; i < bitmap.Width; i++)
 {
   for (int j = 0; j < bitmap.Height; j++)
   {
     int ser = (bitmap.GetPixel(i, j).R + bitmap.GetPixel(i, j).G + bitmap.GetPixel(i, j).B) / 3;
     bitmap.SetPixel(i, j, Color.FromArgb(ser, ser, ser));
   }
 }
return bitmap;
}

 

Code snippet to Replace image in PDF:

private void button1_Click(object sender, EventArgs e)
{ 
 //Loading the existing document.
 PdfLoadedDocument ldoc = new PdfLoadedDocument("input.pdf");
 
 PdfLoadedPageCollection loadedPages = ldoc.Pages;
 
 //iterate through the page.
 foreach (PdfLoadedPage lpage in loadedPages)
 {
  //Extracting images form Page.
  Image[] images = lpage.ExtractImages();
  for (int i = 0; i < lpage.ImagesInfo.Length; i++)
   {
    Bitmap map = new Bitmap(images[i]);
   //Converting color image to black white/gray image.
   PdfImage image = PdfImage.FromImage(ConvertToBlackandWhite(map));
   //Replace the image in the page.
   lpage.ReplaceImage(i, image);
  }
 }
 
 //saving the document
 ldoc.Save("output.pdf");
 ldoc.Close(true);
}

 

Sample:

‚Äčhttps://www.syncfusion.com/downloads/support/directtrac/general/ze/Replace_Image-1538519391

 

Conclusion

I hope you enjoyed learning about how to convert color image to black and white/gray image in WinForms PDF.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment