C
C#2y ago
LukeZurg22

Replacing A Specific Colour of A WriteableBitmap [Answered]

Good morning, I am currently working on a method that is meant to search a WriteableBitmap image for a colour and replace it.
2 Replies
LukeZurg22
LukeZurg222y ago
Below is the method in question that i'm working with. How this works is that the provinceColor is found in the image, and from provinceColor I extrapolate a new countryColor which is meant to be redrawn over a countryMap. The countryMap/outputMap != the provinceMap/givenMap..
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
var pixels = (uint*)givenMap.BackBuffer;
var overWrittenMapPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = pixels[index];

if (rawPixel == provinceColor)
{
pixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Lock();

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = overWrittenMapPixels[index];

if (rawPixel == newColor)
{
overWrittenMapPixels[index] = newColor;
}

});

overWrittenMap.Unlock();
return overWrittenMap;
}
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
var pixels = (uint*)givenMap.BackBuffer;
var overWrittenMapPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = pixels[index];

if (rawPixel == provinceColor)
{
pixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Lock();

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = overWrittenMapPixels[index];

if (rawPixel == newColor)
{
overWrittenMapPixels[index] = newColor;
}

});

overWrittenMap.Unlock();
return overWrittenMap;
}
Problem solved. I was not conscious of the pixels being used. The method below works perfectly fine.
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
overWrittenMap.Lock();
var givenPixels = (uint*)givenMap.BackBuffer;
var overWrittenPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = givenPixels[index];

if (rawPixel == provinceColor)
{
overWrittenPixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Unlock();

return overWrittenMap;
}
public unsafe WriteableBitmap ReplaceColor(WriteableBitmap givenMap, WriteableBitmap overWrittenMap, uint provinceColor)
{

givenMap.Lock();
overWrittenMap.Lock();
var givenPixels = (uint*)givenMap.BackBuffer;
var overWrittenPixels = (uint*)overWrittenMap.BackBuffer;
var pixelCount = givenMap.PixelWidth * givenMap.PixelHeight;

ModData.GetColorsToProvinceIDs().TryGetValue(provinceColor, out var provinceID);
ModData.GetProvinces().TryGetValue((uint)provinceID, out var province);
ModData.GetTagsToCountryNames().TryGetValue(province.Owner, out var countryTAG);
ModData.GetCountryNamesToColours().TryGetValue(countryTAG, out var countryColor);
var newColor = GetRawColor(countryColor);

Parallel.For(0, pixelCount, (index) =>
{
var rawPixel = givenPixels[index];

if (rawPixel == provinceColor)
{
overWrittenPixels[index] = newColor;
}

});

givenMap.Unlock();
overWrittenMap.Unlock();

return overWrittenMap;
}
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
Compiler error [Answered]So im getting compiler error, i understand what it means but i dont know why i get it can you explai[EFCore] add many Parent entities with Child entities, How to connect them in single SaveChangesHello, im trying to sync data in my database with data in external source there is a example that i GroupBy cannot be translated usingHello I can't for example group data by month using one of class datetime attribute navigation propeMicrosoft.Xaml.Behaviors.Wpf MouseMove event passing event args to command is passing nullI have wpf application view. It is capturing MouseMove event, which is sending MouseEventArgs as theEnvironmental Variables in VS 2022 [Answered]```js { "profiles": { "Library.Dependency": { "commandName": "Project", "environmeNeed help getting cest timezonei currently have this code ```cs var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)); wHei all, I am new to this Server and I realy need help on specific Problem [Answered]I am trying to move a .csv File from a Folder to another and it is actualy working but when I open tHow to send multiple data sources to a view in asp.net 6? [Answered]Hey, So im learning asp.net 6. I have a page where i show products by giving a list to the view, ButUncaught Exception C++ hostfxr HandlingHi I have a C++ dll that I am using to host some dotnet assembly and normally things work but when sEntity Data Model WizardI was watching this tutorial: https://www.youtube.com/watch?v=1oGxPoTGl0U&ab_channel=TekTuition Aro