![]() Luckily, solving this problem is simple to fix! However, you may notice that Photoshop doesn’t always allow you to save your image as a PNG file. G.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),Ġ, 0, original.Width, original.Height, GraphicsUnit.Once you’ve created a graphic or transparent background, your next step is to save it as a PNG file. draw original image on the new image using the color matrix ImageAttributes attributes = new ImageAttributes() now is set to red, but with different valuesĬolorMatrix colorMatrix = new ColorMatrix( Graphics g = Graphics.FromImage(newBitmap) get a graphics object from the new Image ![]() create a blank bitmap the same size as originalīitmap newBitmap = new Bitmap(original.Width, original.Height) Convert time is 62 ms: public Bitmap Color(Bitmap original) I will give you another solution since this does not calculate for every pixel. IMO any optimization here is pretty useless: if you need performance use first solution. 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). Here is the Solution I have done with Pixels.Īttaching the source code so one can try the exact and get the result. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |