Add padding to bitmap programmatically - c#

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).

Related

Convert bitmap to threshold (pure black and white)

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
}

Removing image overlay using mask image

I'm writing a program that removes an overlay from a png image using the mask (the overlay image)
having image 1 and 2 I want to achive image 3.
I have tried using lockbits and tried many things but I can't do the math right I think
rgbValues is the byte array of overlay and rgbValues2 is the byte array of given image.
for (int counter = 0; counter < rgbValues.Length; counter ++)
{
int x = (counter / 4) * 4;
if (rgbValues[x + 3] != 0)
{
if (rgbValues[x + 3] == rgbValues2[x + 3])
{
rgbValues2[counter] = 0;
}
else
{
float a1 = (float)rgbValues[counter];
float a2 = (float)rgbValues2[counter] ;
float b1 = (float)rgbValues[x + 3];
float b2 = (float)rgbValues2[x + 3];
rgbValues2[counter] = (byte)(2 * a2- a1);
}
}
}
I've tried this with your sample Images, although they are composed in the same big image and looks like it works. The following code doesn't use LockBits for simplicity, it just gives you the idea, that is how to calculate the base color (in the third image) from the blend color (in the second image) and the result color (in the first image):
public Image ExtractBaseImage(Bitmap resultImage, Bitmap blendImage) {
Bitmap bm = new Bitmap(resultImage.Width, resultImage.Height);
for (int i = 0; i < resultImage.Width; i++) {
for (int j = 0; j < resultImage.Height; j++) {
Color resultColor = resultImage.GetPixel(i, j);
Color blendColor = blendImage.GetPixel(i, j);
if (blendColor.A == 0) bm.SetPixel(i, j, resultColor);
else if(blendColor != resultColor){
float opacity = blendColor.A / 255f;
int r = Math.Max(0,Math.Min(255,(int) ((resultColor.R - (opacity) * blendColor.R) / (1-opacity))));
int g = Math.Max(0,Math.Min(255,(int)((resultColor.G - (opacity) * blendColor.G) / (1-opacity))));
int b = Math.Max(0,Math.Min(255,(int)((resultColor.B - (opacity) * blendColor.B) / (1-opacity))));
bm.SetPixel(i,j,Color.FromArgb(r,g,b));
}
}
}
return bm;
}
Usage: Suppose the images are numbered as you did to the images posted in your question, we have image1, image2, image3 variables:
image3 = ExtractBaseImage((Bitmap)image1, (Bitmap)image2);

How to convert from System.Drawing.Bitmap to grayscale, then to array of doubles (Double[,]), then back to grayscale Bitmap?

I need to perform some mathematical operations in photographs, and for that I need the floating point grayscale version of an image (which might come from JPG, PNG or BMP files with various colordepths).
I used to do that in Python using PIL and scipy.ndimage, and it was very straightforward to convert to grayscale with PIL and then to an array of floating-point numbers with numpy, but now I need to do something similar in C#, and I'm confused how to do so.
I have read this very nice tutorial, that seems to be a recurring reference, but that only covers the "convert to grayscale" part, I am not sure how to get an array of doubles from a Bitmap, and then (at some moment) to convert it back to System.Drawing.Bitmap for viewing.
I'm sure there are loads of optimal ways to do this.
As #Groo points out perfectly in the comments section, one could use for instance the LockBits method to write and read pixel colors to and from a Bitmap instance.
Going even further, one could use the graphics card of the computer to do the actual computations.
Furthermore, the method Color ToGrayscaleColor(Color color) which turns a color into its
grayscale version is not optically correct. There is a set of ratios which actually need to be applied to the color component strengths. I just used 1, 1, 1 ratios. That's accceptable for me and probably horrible for an artist or a scientist.
In the comments section, #plinth was very nice to point out to this question you should look at, if you want to make an anatomically correct conversion: Converting RGB to grayscale/intensity
Just wanted to share this really easy to understand and implement solution:
First a little helper to turn a Color into it's grayscale version:
public static Color ToGrayscaleColor(Color color) {
var level = (byte)((color.R + color.G + color.B) / 3);
var result = Color.FromArgb(level, level, level);
return result;
}
Then for the color bitmap to grayscale bitmap conversion:
public static Bitmap ToGrayscale(Bitmap bitmap) {
var result = new Bitmap(bitmap.Width, bitmap.Height);
for (int x = 0; x < bitmap.Width; x++)
for (int y = 0; y < bitmap.Height; y++) {
var grayColor = ToGrayscaleColor(bitmap.GetPixel(x, y));
result.SetPixel(x, y, grayColor);
}
return result;
}
The doubles part is quite easy. The Bitmap object is a memory representation of the actual image which you can use in various operations. The colordepth and image format details are only the concern of loading and saving instances of Bitmap onto streams or files. We needn't care about those at this point:
public static double[,] FromGrayscaleToDoubles(Bitmap bitmap) {
var result = new double[bitmap.Width, bitmap.Height];
for (int x = 0; x < bitmap.Width; x++)
for (int y = 0; y < bitmap.Height; y++)
result[x, y] = (double)bitmap.GetPixel(x, y).R / 255;
return result;
}
And turning a double array back into a grayscale image:
public static Bitmap FromDoublesToGrayscal(double[,] doubles) {
var result = new Bitmap(doubles.GetLength(0), doubles.GetLength(1));
for (int x = 0; x < result.Width; x++)
for (int y = 0; y < result.Height; y++) {
int level = (int)Math.Round(doubles[x, y] * 255);
if (level > 255) level = 255; // just to be sure
if (level < 0) level = 0; // just to be sure
result.SetPixel(x, y, Color.FromArgb(level, level, level));
}
return result;
}
The following lines:
if (level > 255) level = 255; // just to be sure
level < 0) level = 0; // just to be sure
are really there in case you operate on the doubles and you want to allow room for little mistakes.
The final code, based mostly in tips taken from the comments, specifically the LockBits part (blog post here) and the perceptual balancing between R, G and B values (not paramount here, but something to know about):
private double[,] TransformaImagemEmArray(System.Drawing.Bitmap imagem) {
// Transforma a imagem de entrada em um array de doubles
// com os valores grayscale da imagem
BitmapData bitmap_data = imagem.LockBits(new System.Drawing.Rectangle(0,0,_foto_franjas_original.Width,_foto_franjas_original.Height),
ImageLockMode.ReadOnly, _foto_franjas_original.PixelFormat);
int pixelsize = System.Drawing.Image.GetPixelFormatSize(bitmap_data.PixelFormat)/8;
IntPtr pointer = bitmap_data.Scan0;
int nbytes = bitmap_data.Height * bitmap_data.Stride;
byte[] imagebytes = new byte[nbytes];
System.Runtime.InteropServices.Marshal.Copy(pointer, imagebytes, 0, nbytes);
double red;
double green;
double blue;
double gray;
var _grayscale_array = new Double[bitmap_data.Height, bitmap_data.Width];
if (pixelsize >= 3 ) {
for (int I = 0; I < bitmap_data.Height; I++) {
for (int J = 0; J < bitmap_data.Width; J++ ) {
int position = (I * bitmap_data.Stride) + (J * pixelsize);
blue = imagebytes[position];
green = imagebytes[position + 1];
red = imagebytes[position + 2];
gray = 0.299 * red + 0.587 * green + 0.114 * blue;
_grayscale_array[I,J] = gray;
}
}
}
_foto_franjas_original.UnlockBits(bitmap_data);
return _grayscale_array;
}

C# Bitmap difficulties

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;
}

C# - Crop Transparent/White space

I'm trying to remove all white or transparent pixels from an image, leaving the actual image (cropped). I've tried a few solutions, but none seem to work. Any suggestions or am I going to spend the night writing image cropping code?
So, what you want to do is find the top, left most non white/transparent pixel and the bottom, right most non white/transparent pixel. These two coordinates will give you a rectangle that you can then extract.
// Load the bitmap
Bitmap originalBitmap = Bitmap.FromFile("d:\\temp\\test.bmp") as Bitmap;
// Find the min/max non-white/transparent pixels
Point min = new Point(int.MaxValue, int.MaxValue);
Point max = new Point(int.MinValue, int.MinValue);
for (int x = 0; x < originalBitmap.Width; ++x)
{
for (int y = 0; y < originalBitmap.Height; ++y)
{
Color pixelColor = originalBitmap.GetPixel(x, y);
if (!(pixelColor.R == 255 && pixelColor.G == 255 && pixelColor.B == 255)
|| pixelColor.A < 255)
{
if (x < min.X) min.X = x;
if (y < min.Y) min.Y = y;
if (x > max.X) max.X = x;
if (y > max.Y) max.Y = y;
}
}
}
// Create a new bitmap from the crop rectangle
Rectangle cropRectangle = new Rectangle(min.X, min.Y, max.X - min.X, max.Y - min.Y);
Bitmap newBitmap = new Bitmap(cropRectangle.Width, cropRectangle.Height);
using (Graphics g = Graphics.FromImage(newBitmap))
{
g.DrawImage(originalBitmap, 0, 0, cropRectangle, GraphicsUnit.Pixel);
}
public Bitmap CropBitmap(Bitmap original)
{
// determine new left
int newLeft = -1;
for (int x = 0; x < original.Width; x++)
{
for (int y = 0; y < original.Height; y++)
{
Color color = original.GetPixel(x, y);
if ((color.R != 255) || (color.G != 255) || (color.B != 255) ||
(color.A != 0))
{
// this pixel is either not white or not fully transparent
newLeft = x;
break;
}
}
if (newLeft != -1)
{
break;
}
// repeat logic for new right, top and bottom
}
Bitmap ret = new Bitmap(newRight - newLeft, newTop - newBottom);
using (Graphics g = Graphics.FromImage(ret)
{
// copy from the original onto the new, using the new coordinates as
// source coordinates for the original
g.DrawImage(...);
}
return ret
}
Note that this function will be slow as dirt. GetPixel() is unbelievably slow, and accessing the Width and Height properties of a Bitmap inside a loop is also slow. LockBits would be the proper way to do this - there are tons of examples here on StackOverflow.
Per-pixel check should do the trick. Scan each line to find empty line from the top & bottom, scan each row to find left & right constraints (this can be done in one pass with either rows or columns). When the constraint is found - copy the part of the image to another buffer.
In WPF we have a WriteableBitmap class. Is this what are you looking for ? If it is the case please have a look at http://blogs.msdn.com/b/jgalasyn/archive/2008/04/17/using-writeablebitmap-to-display-a-procedural-texture.aspx
I found a method to batch trim a few thousand .jpg files in about 10 minutes, but I didn't do it in code. I used the Convert feature of Snag-It Editor. I don't know if this is an option for you, if you need to do this trimming once or your need is ongoing, but for the price of the software, which isn't a whole lot, I considered this a decent workaround.
(I do not work for or represent Techsmith.)
Joey
Adding to this, if you are in WPF and you have excess space around your image, check the properties of the image and make sure your Stretch property is set to fill. This eliminated the space around the image.
Screen shot of the property in WPF

Categories