Free Support Forum -

ReplaceColor not working as expected


I’m trying to use ReplaceColor to replace color of PNG files but it seems not to be working properly.
Resulting color is not the right one.
If anyone could give a hand ? Thanks a lot !!

Please find :

  • The link to the image I’m using :

  • The code :

           String OriginalPath = @"C:\temp\icon.png";
          String NewPath = @"C:\temp\icon-2.png";
          String soldColor = "#3173a1";
          String snewColor = "#31a153";
          Aspose.Imaging.License license = new Aspose.Imaging.License();
          System.Drawing.Color oldColor = System.Drawing.ColorTranslator.FromHtml(soldColor);
          System.Drawing.Color newColor = System.Drawing.ColorTranslator.FromHtml(snewColor);
          Aspose.Imaging.Color asposeOldColor = Aspose.Imaging.Color.FromArgb(oldColor.R, oldColor.G, oldColor.G);
          Aspose.Imaging.Color asposeNewColor = Aspose.Imaging.Color.FromArgb(newColor.R, newColor.G, newColor.G);
          using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(OriginalPath))
              Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;
              rasterImage.ReplaceColor(asposeOldColor, 155, asposeNewColor);
              rasterImage.Save(NewPath, true);
          Response.Write(@"ORIGINAL :<br /><img alt='' src=""data:image/png;base64, " + FileInfoToBase64(new System.IO.FileInfo(OriginalPath)) + @""" /> <span style='display:inline-block;width:128px;height:128px;background-color:" + soldColor + @"'>&nbsp;</span>");
          Response.Write(@"<br /><br />NEW :<br /><img alt='' src=""data:image/png;base64, " + FileInfoToBase64(new System.IO.FileInfo(NewPath)) + @""" /> <span style='display:inline-block;width:128px;height:128px;background-color:" + snewColor + @"'>&nbsp;</span>");

Gosh I found, the color wasn’t OK because I used color.G instead of B…


Yes, you have rightly found the mistake in following lines of code.

Please share with us if there is any issue incurring on your end.