473,802 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace all instances of a color in a picturebox

I've created a user control inheriting a picturebox. I want to set a
non-rectangular area to the current system color.control.

I'm looking for either a "fill" command that will set all contiguous
pixels of the same color to a new color, or possibly a command that
changes all pixels of a color to a new color; the only instance of
color.control in the picture is in the background area.

There is a C# edge detection routine that I found on bobpowell.net
(which is an excellent graphics source)so that I could probably use to
build a fill path, but I figure that there must be an easier way.

Thanks in advance.

Nov 21 '05 #1
2 4497
"Shane Stewart" <sh***********@ yahoo.com> schrieb:
I've created a user control inheriting a picturebox. I want to set a
non-rectangular area to the current system color.control.

I'm looking for either a "fill" command that will set all contiguous
pixels of the same color to a new color, or possibly a command that
changes all pixels of a color to a new color; the only instance of
color.control in the picture is in the background area.


Create a 'Bitmap' object of appropriate size, obtain a 'Graphics' object
using 'Graphics.FromI mage', fill the image in the desired color, call
'MakeTransparen t' on the source bitmap in order to make the pixels filled
with the color that should be replaced transparent. Then use the 'Graphics'
object's 'DrawImage' method to draw the transparent bitmap ontp the
destination bitmap. Call the bitmaps 'Save' method in order to save the
file to disk, or display it using a picturebox control.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
Hi folks ! :O)

I ain't know much about GDI+ yet so I'd like your input about this code. ie.
is there any speed and/or memory issues ?

This sample change to black any color the user clicks.
'***
Option Explicit On

Imports System.Drawing
Imports System.Drawing. Imaging
Imports System.Runtime. InteropServices

Public Class Form1
Inherits System.Windows. Forms.Form

<DllImport("gdi 32.dll")> _
Public Shared Function BitBlt( _
ByVal hdcDest As IntPtr, _
ByVal nXDest As Int32, _
ByVal nYDest As Int32, _
ByVal nWidth As Int32, _
ByVal nHeight As Int32, _
ByVal hdcSrc As IntPtr, _
ByVal nXSrc As Int32, _
ByVal nYSrc As Int32, _
ByVal dwRop As Int32 _
) As Boolean
End Function
Private Shared ReadOnly SRCCOPY As Int32 = &HCC0020

' Windows Designer's generated code region goes here

Protected Overrides Sub OnPaint(ByVal e As
System.Windows. Forms.PaintEven tArgs)

MyBase.OnPaint( e)

' draw something
e.Graphics.Fill Rectangle(Brush es.Maroon, 10, 10, 100, 100)
e.Graphics.Fill Ellipse(Brushes .Blue, 10, 10, 100, 100)
e.Graphics.Fill Ellipse(Brushes .DarkBlue, 20, 20, 80, 80)
e.Graphics.Fill Rectangle(Brush es.Coral, 75, 75, 50, 50)

End Sub

Private Sub Form1_MouseDown (ByVal sender As Object, ByVal e As
System.Windows. Forms.MouseEven tArgs) Handles MyBase.MouseDow n

Dim bmp As Bitmap = CreateBitmapFro mControl(Me)
Dim nSearchCol As Int32 = bmp.GetPixel(e. X, e.Y).ToArgb()
Dim nReplaceCol As Int32 = Color.Black.ToA rgb()
Dim bd As BitmapData = bmp.LockBits(Ne w Rectangle(0, 0, bmp.Width,
bmp.Height), Imaging.ImageLo ckMode.ReadWrit e, bmp.PixelFormat )

' loop through the bitmap data and replace
' the matching pixels with black
For i As Int32 = 0 To (bd.Stride * bd.Height) - 1 Step
Marshal.SizeOf( GetType(Int32))
If (Marshal.ReadIn t32(bd.Scan0, i) = nSearchCol) Then
Marshal.WriteIn t32(bd.Scan0, i, nReplaceCol)
End If
Next

bmp.UnlockBits( bd)
Me.CreateGraphi cs().DrawImage( Image.FromHbitm ap(bmp.GetHbitm ap()),
0, 0)

End Sub

Private Function CreateBitmapFro mControl(ByVal ctl As Control) As Bitmap

Dim gCtl As Graphics = ctl.CreateGraph ics()
Dim bmp As Bitmap = New Bitmap(ctl.Clie ntRectangle.Wid th,
ctl.ClientRecta ngle.Height, gCtl)
Dim gMem As Graphics = Graphics.FromIm age(bmp)
Dim hCtlHDC As IntPtr = gCtl.GetHdc()
Dim hMemHDC As IntPtr = gMem.GetHdc()

BitBlt(hMemHDC, 0, 0, ctl.ClientRecta ngle.Width,
ctl.ClientRecta ngle.Height, hCtlHDC, 0, 0, SRCCOPY)

gCtl.ReleaseHdc (hCtlHDC)
gMem.ReleaseHdc (hMemHDC)

Return bmp

End Function

End Class
'***
--
Best Regards
Yanick
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
12399
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all instances of the word "blah" to <img src="MyPicture.jpg" >
3
8898
by: gregpinero | last post by:
I'm trying to write a little script that will have a list of word pairs which will loop through that list and replace all instances of each word with the other word. I'm very new to javascript so I'm not quite sure where to get started. Here is my python "prototype" in case you want a better idea of what I'm trying to do: <code> corpus="""
3
1693
by: Epetruk | last post by:
If I have a snippet of javascript code like this: var str = "This is a snippet; str.replace(/ */gi,' '); what should the call to replace do? My actual tests show that it inserts spaces between each letter... but this doesn't make sense to me. From the regex, it looks like it should be replacing any occurrence of a space followed by any character with a space, meaning that we should have something like 'This s nippet'.
3
2448
by: DDK | last post by:
I am trying to figure out how to Replace tags such as ... with the correct HTML <b>...</b> tags in C#. The code below works however only if one set of tags are found, if you have more than two sets of the tags within the text it renders from the first . Here is the code that doesn't quite work: //////////////////////////////////////////////////////////////////////////// /////
18
1571
by: Jon S via DotNetMonster.com | last post by:
Hi all, I searching a string to see if "Desc" resides in it, if so then replace it with "Description". For some reason if it finds "Desc" in the word "Description" it replaces the "Desc" part of it with "Description thus having the final outcome of "Descriptionription". Is there a way to have the replace() method only replace if the entire string is "Desc" only and not "Desc* " ? Hope this makes sense. Thanks in advance. if(...
23
1950
by: digitalorganics | last post by:
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj = NewClass() newobj.__dict__.update(object.__dict__) return newobj
0
2079
by: Kevin Blount | last post by:
I found an alternative to string.Replace(...) that will ignore case when looking for things to search, and while it's working a lot better than a standard .Replace(...) it's not working exactly how I'd like. Here's the code I'm now using: static string CIR(string input, string oldValue, string newValue) { Regex regEx = new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Singleline);
17
8137
by: alxasa | last post by:
Hi, can someone please show me how to most elegently do this?..... I have a textbox, and I want to search the contents of it and replace all instances of a certain word, and replace that word with something else. For the purposes of this it could be replacing "green" with "blue". Can someone please show me how to properly do this? :) Sincerest regards, Alxasa.
18
2483
by: Umesh | last post by:
Do you have any answer to it? thx.
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10535
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10282
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10061
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9111
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.