Image class in C# - c#-2.0

Can anyone help me out in setting blank image using Image class in C#? I mean, i have few images to be displayed based on some manipulations.If any condition is met, i shouldn't display any image in tat case.And i dont hav any default image to display for the above said scenario.How can i implement this?

Like this?
Image blank = new Bitmap(width, height);
This will produce a new image with the specified width and height (integers).
If you want the image to have a specific color, you will need to set each pixel with SetPixel:
// Set each pixel in blank to black. Use a different Color.xxx if you wish
for (int Xcount = 0; Xcount < myBblankitmap.Width; Xcount++)
{
for (int Ycount = 0; Ycount < blank.Height; Ycount++)
{
blank.SetPixel(Xcount, Ycount, Color.Black);
}
}
Alternatively, you can create an actual default image (using an image editor of your choice) and display that.

Related

Finding colour of specific pixel on UIImageView

I am working on a paint application and want to build the undo manager. I store the pixel co-ordinate value of each location where the user draws but I also want to store the old pixel colour of the point where the user has drawn so that I can undo it to appropriate colour. But I am unable to do so. Can anybody help.
Here is the code I am currently using to get the pixel colour:
UIGraphicsBeginImageContext(self.tempDrawImage.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
int bpr = CGBitmapContextGetBytesPerRow(context);
unsigned char * data = CGBitmapContextGetData(context);
if (data!=NULL)
{
int offset = bpr*(lastPoint.y)+ 4*(lastPoint.x);
NSLog(#"Red : %d",data[offset+0]);
NSLog(#"Green : %d",data[offset+1]);
NSLog(#"Blue : %d",data[offset+2]);
}
Best solution for this to use UIImage+ColorAtPixel category class of UIImage. I have used this, very nice and perfect.
You can create the copy of your image every time before image drawing modifications begin (for example just before a user taps on the screen).

Adding a gif to a larger background

I'm using imagemagick to resize uploaded files but while they're processing I want to show a rotating gif wheel to the user where the thumbnail would normally be. I serve about 7 sizes of thumbs and would like the wheel to remain at it's 32x32 size in the middle, that's the simple bit.
What I need to know is, can I do the above while still retaining the animation
Example:
This Image:
Starting at this size
With Animation
Check out this fiddle, it might contain what you want: http://jsfiddle.net/TGdFB/1/
It uses jQuery, but should be easily adaptable...
Ended up doing this manually by Photoshop after not being able to find an automated way of doing this through imagemagick. I found the 'coalesce' flag but not much else.
there is a solution in php witch i use to watermark gif animated images ....
it create an black bacground put an image on it and then put watermark ...
watermarkpath = 'path to wathermarkimage.jpg|gif|png';
$imagepath= 'path to the image';
$watermark = new Imagick($watermarkpath);
$GIF = new Imagick();
$GIF->setFormat("gif");
$animation = new Imagick($imagepath);
foreach ($animation as $frame) {
$iWidth = $frame->getImageWidth();
$iHeight = $frame->getImageHeight();
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
if ($iHeight < $wHeight || $iWidth < $wWidth) {
// resize the watermark
$watermark->scaleImage($iWidth, $iHeight);
// get new size
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
}
$bgframe = new Imagick();
$bgframe->newImage(($iWidth), ($iHeight + 80), new ImagickPixel('Black'));
$bgframe->setImageDelay($frame->getImageDelay());
$x = ($iWidth) - $wWidth - 5;
$y = ($iHeight + 80) - $wHeight - 5;
$bgframe->compositeImage($frame, imagick::COMPOSITE_DEFAULT, 0, 0);
$bgframe->flattenImages();
$bgframe->compositeImage($watermark, imagick::COMPOSITE_OVER, $x, $y);
$bgframe->flattenImages();
$GIF->addImage($bgframe);
}
$GIF->writeimages($imagepath,true);

how to embed a watermark on an image using edge in matlab?

in a school project i would like to do the following step to have a watermaked image in matlab
extract the edges from an image
insert a mark on this edge
reconstruct the image
extract the mark
could some one give me a link to have a good idea how to do it or help me to do that?
thank you in advance
You want to add a watermark to an image? Why not just overlay the whole thing.
if you have an image
img = imread('myimage.jpg')
wm = imread('watermark.jpg')
You can just resize the watermark to the size of the image
wm_rs = imresize(wm, [size(img,1) size(img,2)], 'lanczos2');
img_wm(wm_rs ~= 0) = wm_rs; %This sets non-black pixels to be the watermark. (You'll have to slightly modify this for color images)
If you want to put it on the edges of the image, you can extract them like this
edges = edge(rgb2gray(img),'canny')
Then you can set the pixels where the edges exist to be watermark pixels
img_wm = img;
img_wm(edges ~= 0) = wm_rs(edges~=0);
Instead of direct assignment you can play around with using a mix of the img and wm_rs pixel values if you want transparency.
You'll probably have to adjust some of what I said to color images, but most should be the same.
Here, is a nice and simple example how you can embed watermarks using MATLAB (in the spatial domain): http://imageprocessingblog.com/digital-watermarking/
see example below(R2017b or later release):
% your params
img = imread('printedtext.png');
Transparency = 0.6;
fontColor = [1,1,1]; % RGB,range [0,1]
position = [700,200];
%% add watermark
mask = zeros(size(img),'like',img);
outimg = insertText(mask,position,'china', ...
'BoxOpacity',0,...
'FontSize',200,...
'TextColor', 'white');
bwMask = imbinarize(rgb2gray(outimg));
finalImg = labeloverlay(img,bwMask,...
'Transparency',Transparency,...
'Colormap',fontColor);
imshow(finalImg)

how to do image resize without distortion in Blackberry

I am downloading an image from the web and applying fixed32 method to resize the image to fit to screen. Resizing the image causes some loss of clarity, but my client wants to display the image without this distortion. How can I do this?
The way I do this is to not resize the image in the phone. I request my server to send me image based on the width*height I need. The server then resizes the image for me. How about you try a similar solution. This would help you by providing a proper resized image, and improve performance of your app.
Use following method for resizing image.
public static Bitmap SizePic (EncodedImage Resizor,int Height, int Width) {
int multH;
int multW;
int currHeight = Resizor.getHeight();
int currWidth = Resizor.getWidth();`enter code here`
multH= Fixed32.div(Fixed32.toFP(currHeight),Fixed32.toFP( Height));
multW = Fixed32.div(Fixed32.toFP(currWidth),Fixed32.toFP(Width));
Resizor = Resizor.scaleImage32(multW,multH);
return Resizor.getBitmap();
}

how to optimized this image processing replace all pixels on image with closest available RGB?

Im' trying to replace all pixels of input image with closest available RGB. I have a array contain color and input image. Here is my code, it give me an output image as expected, BUT it take very LONG time( about a min) to process one image. Can anybody help me improve the code? Or if you have any other suggestions, please help.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGImageGetWidth(sourceImage),CGImageGetHeight(sourceImage)), NO, 0.0f);
//Context size I keep as same as original input image size
//Otherwise, the output will be only a partial image
CGContextRef context;
context = UIGraphicsGetCurrentContext();
//This is for flipping up sidedown
CGContextTranslateCTM(context, 0, self.imageViewArea.image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// init vars
float d = 0; // squared error
int idx = 0; // index of palette color
int min = 1000000; // min difference
UIColor *oneRGB; // color at a pixel
UIColor *paletteRGB; // palette color
// visit each output color and determine closest color from palette
for(int y=0; y<sizeY; y++) {
for(int x=0; x<sizeX; x++) {
// desired (avg) color is one pixel of scaled image
oneRGB = [inputImgAvg colorAtPixel:CGPointMake(x,y)];
// find closest color match in palette: init idx with index
// of closest match; keep track of min to find idx
min = 1000000;
idx = 0;
CGContextDrawImage(context,CGRectMake(xx, yy, 1, 1),img);
}
}
UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageViewArea.image = output;
This is a similar question (with no definitive answer), but the answer there has the code for directly accessing pixels from an image.
Quantize Image, Save List of Remaining Colors
You should do that rather than use CG functions for each get and set pixel. Drawing 1 pixel of an image onto another image is a lot slower than changing 3 bytes in a array.
Also, what's in ColorDiff -- you don't need perfect diffing as long as the closest pixel has the smallest diff. There may be room for pre-processing this list so that for each palette entry you have the smallest diff to the nearest other palette entry. Then, while looping through pixels, I can quickly check to see if the next pixel is within half that distance to the color just found (because photos tend to have common colors near each other).
If that's not a match, then while looping through the palette, if I am within half this distance to any entry, there is no need to check further.
Basically, this puts a zone around each palette entry where you know for sure that this one is the closest.
The usual answer is to use a k-d tree or some other Octree structure to reduce the number of computations and comparisons that have to be done at each pixel.
I've also had success with partitioning the color space into a regular grid and keeping a list of possible closest matches for each part of the grid. For example you can divide the (0-255) values of R,G,B by 16 and end up with a grid of (16,16,16) or 4096 elements altogether. Best case is that there's only one member of the list for a particular grid element and no need to traverse the list at all.

Resources