public Bitmap rotateImage()
{
try
{
curImgHndl.CurrentRotationHandler.Flip(RotateFlipType.Rotate90FlipNone);
}
catch (Exception ex)
{
}
return objCurrImageHandler.CurrentBitmap;
}
When an image is rotated several times (5 or more) using this function then it shows the error message
“Out Of Memory” .
To ratate an image in c#.net 4 I used ImageFunctions.dll. Decompiling the dll I have got the followings.
only a portion of the whole code is given that is used for rotation
public class RotationHandler
{
private ImageHandler imageHandler;
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();
bitmap.RotateFlip(rotateFlipType);
this.imageHandler.CurrentBitmap = (Bitmap) bitmap.Clone();
}
}
How can I solve it?
the following method solve the problem as lazyberezovsky suggested.
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
this.imageHandler.CurrentBitmap.RotateFlip(rotateFlipType);
}
But another problem in brightness method.
public void SetBrightness(int brightness)
{
Bitmap temp = (Bitmap)_currentBitmap;
Bitmap bmap = (Bitmap)temp.Clone();
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + brightness;
int cG = c.G + brightness;
int cB = c.B + brightness;
if (cR < 0) cR = 1;
if (cR > 255) cR = 255;
if (cG < 0) cG = 1;
if (cG > 255) cG = 255;
if (cB < 0) cB = 1;
if (cB > 255) cB = 255;
bmap.SetPixel(i, j, Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
}
}
_currentBitmap = (Bitmap)bmap.Clone();
}
This method works for some images and doesn't work for other images and shows the following error
"SetPixel is not supported for images with indexed pixel formats."
It would be very much helpful if you could provide efficient and workable methods for rotation, crop and brightness.
please help again.
As Claudio mentions, you need to make sure you are disposing of any unused Bitmaps.
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();
this.imageHandler.CurrentBitmap.Dispose(); // dispose of current bitmap
bitmap.RotateFlip(rotateFlipType);
Bitmap clone_map = (Bitmap) bitmap.Clone();
bitmap.Dipose(); // dispose of original cloned Bitmap
this.imageHandler.CurrentBitmap = clone_map;
}
You could probably simplify this to just:
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
Bitmap bitmap = (Bitmap) this.imageHandler.CurrentBitmap.Clone();
this.imageHandler.CurrentBitmap.Dispose(); // dispose of current bitmap
bitmap.RotateFlip(rotateFlipType);
this.imageHandler.CurrentBitmap = bitmap;
}
Why not to rotate current bitmap, instead of creating copies?
public class RotationHandler
{
private ImageHandler imageHandler;
public void Flip(RotateFlipType rotateFlipType)
{
this.imageHandler.RestorePrevious();
this.imageHandler.CurrentBitmap.RotateFlip(rotateFlipType);
}
}
Related
It seems, that if image, that is used for icon for Windows shortcuts, doesn't have aspect ratio 1:1, it will look stretched.
Left one is how it actually looks, and right one is how it should look.
I'm creating shortcut and icon programmatically from image, so I want to fix image, so it will have correct aspect ratio, but image will not look stretched. This can be achieved by adding some padding to image.
As for now, I'm simply copying image to new bitmap with correct aspect ratio, but filling new area with transparent pixels
public static Bitmap FixBitmapAspectRatio(Bitmap sourceBitmap)
{
if (sourceBitmap.Width.Equals(sourceBitmap.Height))
return sourceBitmap;
int size;
bool horizontallyOriented;
if (sourceBitmap.Width > sourceBitmap.Height)
{
horizontallyOriented = true;
size = sourceBitmap.Width;
}
else
{
horizontallyOriented = false;
size = sourceBitmap.Height;
}
var sizeDifference = Math.Abs(sourceBitmap.Width - sourceBitmap.Height);
var newBitmap = new Bitmap(size, size);
var transparentColor = Color.FromArgb(0, 0, 0, 0);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (horizontallyOriented)
{
if (i < sizeDifference / 2 || i >= sizeDifference / 2 + sourceBitmap.Height)
{
newBitmap.SetPixel(j, i, transparentColor);
}
else
{
var originalPixel = sourceBitmap.GetPixel(j, i - sizeDifference / 2);
newBitmap.SetPixel(j, i, originalPixel);
}
}
else
{
if (i < sizeDifference / 2 || i >= sizeDifference / 2 + sourceBitmap.Width)
{
newBitmap.SetPixel(i, j, transparentColor);
}
else
{
var originalPixel = sourceBitmap.GetPixel(i - sizeDifference / 2, j);
newBitmap.SetPixel(i, j, originalPixel);
}
}
}
}
return newBitmap;
}
But I don't know, if I'm inventing a wheel. Is there any way to do this by means by standard libraries, or maybe easier way to achieve what I need?
You really don't want to set individual pixels :)
Instead, have a look at the Graphics class, in particular Graphics.FromImage (that's where you paint to) and Graphics.DrawImage (that's how you paint the scaled image).
I have tried this code for converting a bitmap to pure black and white - not greyScale, but this gives me a pure black image.
public Bitmap blackwhite(Bitmap source)
{
Bitmap bm = new Bitmap(source.Width,source.Height);
for(int y=0;y<bm.Height;y++)
{
for(int x=0;x<bm.Width;x++)
{
if (source.GetPixel(x, y).GetBrightness() > 0.5f)
{
source.SetPixel(x,y,Color.White);
}
else
{
source.SetPixel(x,y,Color.Black);
}
}
}
return bm;
}
What can cause such a problem? Is there any alternate method to this?
I know this answer is way too late but I just figured it out and hope it helps other people having this problem.
I get the average brightness of the picture and use that as the threshold for setting pixels to black or white. It isn't 100% accurate and definitely isn't optimized for time complexity but it gets the job done.
public static void GetBitmap(string file)
{
using (Bitmap img = new Bitmap(file, true))
{
// Variable for image brightness
double avgBright = 0;
for (int y = 0; y < img.Height; y++)
{
for (int x = 0; x < img.Width; x++)
{
// Get the brightness of this pixel
avgBright += img.GetPixel(x, y).GetBrightness();
}
}
// Get the average brightness and limit it's min / max
avgBright = avgBright / (img.Width * img.Height);
avgBright = avgBright < .3 ? .3 : avgBright;
avgBright = avgBright > .7 ? .7 : avgBright;
// Convert image to black and white based on average brightness
for (int y = 0; y < img.Height; y++)
{
for (int x = 0; x < img.Width; x++)
{
// Set this pixel to black or white based on threshold
if (img.GetPixel(x, y).GetBrightness() > avgBright) img.SetPixel(x, y, Color.White);
else img.SetPixel(x, y, Color.Black);
}
}
// Image is now in black and white
}
I am trying to make an image black and white in c#. Here is my code:
public static void SetGrayscale(Bitmap b)
{
Bitmap temp = (Bitmap) b;
Bitmap bmap = (Bitmap)temp.Clone();
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);
bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
}
}
b = (Bitmap)bmap.Clone();
}
static void Main(string[] args)
{
Bitmap bm = new Bitmap(Image.FromFile("D:\\users\\visual studio 2010\\Projects\\aaa\\20130924_144411.tif"));
byte[] pixels = ImageToByte(bm);
SetGrayscale(bm);
}
The problem is, it does not turn into black and white, it is still the same. Am i not saving the changed image? What can be the problem here?
Thanks
Are you refering to that the file on disk doesn't change? You would have to save the grayscaled bitmap: bm.Save("D:\\bitmap.tif");
What I am trying to do is check an image row or column, and if it contains all white pixels then trim that row or column.
I am not sure why, but when I run this code snippet, img.Width in TrimLeft is 1, before subtracting 1 from it.
The actual image width is 175. I end up with an ArgumentException. I have a similar method for trimming the right side, and that works fine.
class ImageHandler
{
public Bitmap img;
public List<int[]> pixels = new List<int[]>();
public ImageHandler(String path)
{
img = new Bitmap(path);
GetPixels();
}
public void TrimLeft()
{
while (CheckColIfWhite(0, 0))
{
Rectangle cropBox = new Rectangle(1, 0, (img.Width-1), img.Height);
Bitmap cropdImg = CropImage(img, cropBox);
img = cropdImg;
}
}
public bool CheckColIfWhite(int colx, int starty)
{
bool allPixelsWhite = false;
int whitePixels = 0;
for (int y = starty; y < img.Height; y++)
{
if (pixels[y][colx] >= 200) { whitePixels++; }
else { return false; }
if (whitePixels == img.Height) { allPixelsWhite = true; }
}
return allPixelsWhite;
}
public void GetPixels()
{
for (int y = 0; y < img.Height; y++)
{
int[] line = new int[img.Width];
for (int x = 0; x < img.Width; x++)
{
line[x] = (int) img.GetPixel(x, y).R;
}
pixels.Add(line);
}
}
public Bitmap CropImage(Bitmap tImg, Rectangle area)
{
Bitmap bmp = new Bitmap(tImg);
Bitmap bmpCrop = bmp.Clone(area, bmp.PixelFormat);
return bmpCrop;
}
}
Your method seems like it will be remarkably inefficient - if the image is 175 pixels wide, and entirely white, you're going to create 175 copies of it, before (probably) failing when trying to create a 0 pixel wide image.
Why not examine each column in turn until you find a non-white column, and then perform a single crop at that time. Untested code, and with other changes hopefully obvious:
public Bitmap CropImage (Bitmap image)
{
int top = 0;
int bottom = image.Height-1;
int left = 0;
int right = image.Width-1;
while(left < right && CheckColIfWhite(image,left))
left++;
if(left==right) return null; //Entirely white
while(CheckColIfWhite(image,right)) //Because left stopped, we know right will also
right--;
while(CheckRowIfWhite(image,top))
top++;
while(CheckRowIfWhite(image,bottom))
bottom--;
return CropImage(image,new Rectangle(left,top,right-left+1,bottom-top+1));
}
(E.g. I'm now passing the image around, I've modified CheckColIfWhite and CheckRowIfWhite to take the image also, and to assume that one parameter is always fixed at 0)
Also, not sure why you're extracting the pixel array beforehand, so I'm putting my re-written CheckColIfWhite too:
public bool CheckColIfWhite(Bitmap image,int colx)
{
for (int y = 0; y < image.Height; y++)
{
if (image.GetPixel(colx,y).R < 200)
return false;
}
return true;
}
How do i draw this more efficiently?
I can feel the lag when i call the code below. NOTE this is about pixel editing and not clearing the screen.
int colorIndex = 0;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (colorIndex == 0)
draw(Color.DimGray);
else if(colorIndex ==1)
draw(Color.ForestGreen);
colorIndex++;
colorIndex = colorIndex % 2;
pictureBox1.Invalidate();
//pictureBox1.Update();
}
void draw(Color c)
{
//var bdata = b.LockBits(Rectangle.Empty, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//var px = bdata.Scan0;
var px = b;
{
for (int y = 0; y < b.Height; y++)
{
for (int x = 0; x < b.Width; x++)
//px[y * b.Width + x] = -1;
px.SetPixel(x, y, c);
}
}
//b.UnlockBits(bdata);
}
Have you enable double buffering?
btw, If you are just drawing rectangle, you can use the DrawRectangle method.
How about:
void draw(Color c) {
using (Graphics g = Graphics.FromImage(b)) {
g.Clear(c);
}
}
SetPixel/GetPixel are generally slow operations. If you can use unsafe code (code which uses pointers), there are faster methods of access, but they're slightly more involved. There is a tutorial here which explains how it works, however:
http://www.codeproject.com/KB/GDI-plus/csharpgraphicfilters11.aspx