![]() To ignore alpha you may use something like this: if (color.R = toReplace.R & color.G = toReplace.G & color.B = toReplace.B)įinally if you know that pixels to replace are little you may create a raw copy of original image (using Graphics.FromImage to create a context and to draw into it source bitmap), in such way you'll call SetPixel() only when there is a replacement. Target.SetPixel(x, y, color = toReplace ? replacement : color) Īlso please note that this consider alpha channel in comparison (so 50% transparent green, for example, is not same color as 30% transparent green). Var target = new Bitmap(source.Width, source.Height) static Bitmap ReplaceColor(Bitmap source, Please note that this should be rarely used because it's terribly slow. ![]() ![]() Of course this can be further optimized and you may need to handle different formats ( see this list of pixel formats and this article about their layout) but consider it a starting point to work with bitmaps.įor completeness this is equivalent color without direct access to bitmap data. If (toReplace.R = r & toReplace.G = g & toReplace.B = b) ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb) īyte* sourceRow = (byte*)sourceData.Scan0 + (y * sourceData.Stride) īyte* targetRow = (byte*)targetData.Scan0 + (y * targetData.Stride) New Rectangle(0, 0, target.Width, target.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb) New Rectangle(0, 0, source.Width, source.Height), If you know the image format in advance (and it's fixed for each image) then you can do it much much faster with little bit more code: static unsafe Bitmap ReplaceColor(Bitmap source,Ĭonst int pixelSize = 4 // 32 bits per pixelīitmapData sourceData = null, targetData = null It's much better to handle bitmap data directly. Now you'll see that it works but it takes a very long time to process each image: GetPixel and SetPixel are pretty slow (primary because they check and calculate everything for each call). NewBitmap.SetPixel(i, j, Color.FromArgb(originalColor.A, Color.Red)) If (originalColor.R = 0 & originalColor.G = 0 & originalColor.B = 0) To check for transparency you should compare not the Color object but the R, G, B values, let's change to: var originalColor = scrBitmap.GetPixel(i, j) You do not compare to Color.Black but you assign Color.Black to originalColor.Below is the sample image and different results by applying different colorīefore we talk about perfromance let's check your code: var originalColor = scrBitmap.GetPixel(i, j) if we set all pixel color to new then there will be no smoothness left. make an empty bitmap the same size as scrBitmapīitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height) įor (int i = 0 i 150 because. Public static Bitmap ChangeColor(Bitmap scrBitmap) Destination Directory debug\bin\BigGreen\ String files = Directory.GetFiles("Big\\") I have sample images of 128x128 (Width x Height). Note, while ivan is the filters guru, if you are new to Inkscape, the Filters Editor is probably a bit too advanced for you, imo (unless you know other graphics programs very well).Here is the Solution I have done with Pixels.Īttaching the source code so one can try the exact and get the result. But if it doesn't, you can make it transparent with the Document Properties dialog - instructions in same tutorial. Please read my tutorial: viewtopic.php?f=6&t=6616 It will remove your black background, and I assume make it transparent. As for making the background transparent, Inkscape backgrounds are transparent by default.Īctually in Trace Bitmap, there's the SIOX Foreground Selection option. After you have traced it, you can use the Replace Color extension, to replace the white(Extensions menu > Color > Replace Color). If you do need it vectored, then indeed, Trace Bitmap is your best option. Is there some reason that you particulary need to have in vector format? Because otherwise, what you want to do would better be done in a raster graphics program (such as GIMP), since it's already in raster format (GIF).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |