How to convert color image to black and white/gray image in WinForms PDF?
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); }
https://www.syncfusion.com/downloads/support/directtrac/general/ze/Replace_Image-1538519391
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
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 Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
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 explore 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 forums or feedback portal. We are always happy to assist you!