how to replace a pixel with a set of pixel c# - c#

hi i'm making photo mosaic creator but i can't replace a pixel with a set of pixel if any one know how to do this in c# please help cause i'm beginner in c#
and i understand the idea but i can't implement in c#
the idea is we have a input photo and we have a data set of small photos and we create an empty output photo and we put inside here the small photo from the data set after we take the pixel average RBG color from the input photo and we take the average RBG color of the small photo then we replace the pixel with the photo that have the same RGB color of the pixel and sort the small photo's in the output photo

Well, you don't "replace a pixel with an entire picture" when building a mosaic digitally. Instead, you create a brand new output image that is much larger than the input. Then you think about a grid on the output so that each input pixel corresponds to a grid square in the output. You then copy an image into each grid square, checking the corresponding input pixel to find out what image is appropriate.

I have not used it, but WriteableBitmapEx is a class for creating bitmaps and has a SetPixel method.

Related

Save part of an image in C#?

Well, I'm studying bitmaps, and I downloaded a pdf which said an image was an array of pixels. Since it is an array (which is quite logical) there is how to define points within the image, mark a region say so. The bitmap class as well should know how to read the properties of an image, such as the "size" of it, I wonder if there is any way to score points like 200x200 (example) and save that region into a new image.
The image of Braum, character of the game League of Legends, is random and this mark in black is to show what it would be this region which I mean.
image : https://i.stack.imgur.com/UFCoX.png

How can I draw legible text on a bitmap (Winforms)?

I've written a photo viewer, and I want to superimpose text over the photo. I want the font or font color to make the text as legible as possible on top of the bitmap, no matter what the underlying bitmap looks like.
My current thinking is to take the region of the bitmap where the text will appear, and make some kind of "overall color" calculation for that area, and then set the font color to be something correspondingly contrasting.
However, this math is way over my head. Has anybody seen a method for making this type of "what's the average color of all of these pixels" calculation? Or is that not even the best approach?
EDIT: I'm moving the second portion of this to another question.
You can use this to calculate average color of a region of bitmap:
How to calculate the average rgb color values of a bitmap
Do you store your image as a Bitmap?
You can also draw an outlined text. For example, white text with black outline. This will make text visible on most of backgrounds:
How to Drawing Text with Outline onto Images?

Writing an image file from array of xyz coordinate

I'm currently working on a fractal generator in C# and I'm trying to export my set of coordinates to a very simple image file.
What i have is a 1-dimensional array of 3d point (x, y and z coordinate) and i wish to save it to an image where the z value will be the color of the pixel (grayscale only).
From what i gathered, the RAW format would be the easiest to work with, but even then i couldn't find documentation on it. I then looked at the namespace System.Drawing but i'm at a lost, it seems overly complicated for what i'm trying to achieve.
Is there an easy way to write such an image?
This link should help explain how to create an image pixel by pixel:
http://msdn.microsoft.com/en-us/library/system.drawing.image%28v=vs.80%29.aspx
then you should be able to use image1.Save with a string argument of the file location.
If you just want to display it on a winform, then you could keep the original source code from the link.
You can create a new image instead of loading one like so:
System.Drawing.Bitmap myMap = new System.Drawing.Bitmap(200,300); //200x300 pixels

How to convert an Indexed pixel format image to a 32-bit image?

I have an image that I get and attempt to load into a graphics object using Graphics.FromImage(image), however this throws an exception if the image has an indexed pixel format.
Is there a way to safely convert an indexed image?
Update: Thanks to Joe for the tip to just draw the old image over the new one, instead I was trying to convert it. This makes a lot of sense.
One easy way is to create a new image of the same size (with a 32-bit pixel format). Then create a graphics object for that image and draw the original on top of it.
what you can do is based on the indexes u can calculate the euclidian distance in the 3 channel color space. Then find the closest color and use those values for your new image.

How to convert a 32bpp image to an indexed format?

So here are the details (I am using C# BTW):
I receive a 32bpp image (JPEG compressed) from a server. At some point, I would like to use the Palette property of a bitmap to color over-saturated pixels (brightness > 240) red. To do so, I need to get the image into an indexed format.
I have tried converting the image to a GIF, but I get quality loss. I have tried creating a new bitmap in an index format by these methods:
// causes a "Parameter not valid" error
Bitmap indexed = new Bitmap(orig.Width, orig.Height, PixelFormat.Indexed)
// no error, but the resulting image is black due to information loss I assume
Bitmap indexed = new Bitmap(orig.Width, orig.Height, PixelFormat.Format8bppIndexed)
I am at a loss now. The data in this image is changed constantly by the user, so I don't want to manually set pixels that have a brightness > 240 if I can avoid it. If I can set the palette once when the image is created, my work is done. If I am going about this the wrong way to begin with please let me know.
EDIT: Thanks guys, here is some more detail on what I am attempting to accomplish.
We are scanning a tissue slide at high resolution (pathology application). I write the interface to the actual scanner. We use a line-scan camera. To test the line rate of the camera, the user scans a very small portion and looks at the image.
The image is displayed next to a track bar. When the user moves the track bar (adjusting line rate), I change the overall intensity of the image in an attempt to model what it would look like at the new line rate. I do this using an ImageAttributes and ColorMatrix object currently.
When the user adjusts the track bar, I adjust the matrix. This does not give me per pixel information, but the performance is very nice. I could use LockBits and some unsafe code here, but I would rather not rewrite it if possible. When the new image is created, I would like for all pixels with a brightness value of > 240 to be colored red. I was thinking that defining a palette for the bitmap up front would be a clean way of doing this.
Going from 32bpp to 8bpp indexed will almost always result in quality loss, unless the original image has less than 256 colors total.
Can you create another image that is a overlay with the affected pixels red, then show both of those?
Since you are going for brightness > 240, you can convert the overlay to grayscale first, then to indexed to get the overbright pixels.
You don't specify what you are doing with it once you have tagged the offenders, so I don't know if that will work.
Sounds like something you could do easily with a pixel shader. Even very early shader models would support something as easy as this.
The question is however:
Can you include shader support in your application without too much hastle?
Do you know shader programming?
EDIT:
You probably don't have a 3D context where you can do stuff like this =/
I was mostly just airing my thoughts.
Manipulating the picture pixel by pixel should be doable in real-time with a single CPU shouldn't it?
If not, look into GPGPU programming and Open CL.
EDIT AGAIN:
If you gave some more details about what the app actually does we might help a bit more? For example, if you're making a web-app none of my tips would make sense.
Thanks for the help everyone. It seems that this can be solved using the ImageAttributes class and simply setting a color remap table.
ColorMap[] maps = new ColorMap[someNum]
// add mappings
imageAttrs.SetRemapTable(maps);
Thanks for the help again, at least I learned something.

Categories