473,382 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 4465
"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.FromImage', fill the image in the desired color, call
'MakeTransparent' 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("gdi32.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.PaintEventArgs)

MyBase.OnPaint(e)

' draw something
e.Graphics.FillRectangle(Brushes.Maroon, 10, 10, 100, 100)
e.Graphics.FillEllipse(Brushes.Blue, 10, 10, 100, 100)
e.Graphics.FillEllipse(Brushes.DarkBlue, 20, 20, 80, 80)
e.Graphics.FillRectangle(Brushes.Coral, 75, 75, 50, 50)

End Sub

Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

Dim bmp As Bitmap = CreateBitmapFromControl(Me)
Dim nSearchCol As Int32 = bmp.GetPixel(e.X, e.Y).ToArgb()
Dim nReplaceCol As Int32 = Color.Black.ToArgb()
Dim bd As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width,
bmp.Height), Imaging.ImageLockMode.ReadWrite, 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.ReadInt32(bd.Scan0, i) = nSearchCol) Then
Marshal.WriteInt32(bd.Scan0, i, nReplaceCol)
End If
Next

bmp.UnlockBits(bd)
Me.CreateGraphics().DrawImage(Image.FromHbitmap(bm p.GetHbitmap()),
0, 0)

End Sub

Private Function CreateBitmapFromControl(ByVal ctl As Control) As Bitmap

Dim gCtl As Graphics = ctl.CreateGraphics()
Dim bmp As Bitmap = New Bitmap(ctl.ClientRectangle.Width,
ctl.ClientRectangle.Height, gCtl)
Dim gMem As Graphics = Graphics.FromImage(bmp)
Dim hCtlHDC As IntPtr = gCtl.GetHdc()
Dim hMemHDC As IntPtr = gMem.GetHdc()

BitBlt(hMemHDC, 0, 0, ctl.ClientRectangle.Width,
ctl.ClientRectangle.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
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...
3
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...
3
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...
3
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...
18
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...
23
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 =...
0
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...
17
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...
18
by: Umesh | last post by:
Do you have any answer to it? thx.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.