Image resizing in C# with alignment of a logo and text in the image to the top - c#

I have an image 3519 X 2495 with some logo and text next to it. When the image is opened, i see the logo and the text next to it in the center. I would like to resize the image to 768 X 1004 and want both the logo and the text next to it to appear on the top. When I resize the image I get the logo and text next to it in the center.
Is there a good way to achieve this in c#.
I tried the below code
Image image = Image.FromFile(#"D:\SSH\Automation\ImageResize\Diageo.jpg");
Bitmap bitmap = new Bitmap(768, 1004);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(image, 0, 0, 768, 1004);
bitmap.Save(#"D:\SSH\Automation\ImageResize\Diageo.png");
graphics.Dispose();

To resize an image and keep its initial aspect ratio use the following code:
Note that I use usings from the IDisposable interface instead of calling Dispose myself as this is considered best practice and is safer.
int maxWidth = 768;
int maxHeight = 1004;
using (Bitmap bitmap = new Bitmap(filePath))
{
int width = (int)(bitmap.Width * (maxHeight / (float)bitmap.Height));
int height = maxHeight;
if (bitmap.Height * (maxWidth / (float)bitmap.Width) <= maxHeight)
{
width = maxWidth;
height = (int)(bitmap.Height * (maxWidth / (float)bitmap.Width));
}
using (Bitmap resizedBitmap = new Bitmap(width, height))
{
resizedBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
using (Graphics g = Graphics.FromImage(resizedBitmap))
{
g.DrawImage(bitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height);
}
//Use resizedBitmap here
}
}

Related

how to have my rectangle always on the middle of the image without knowing the size of the incomming image

Trying to add a watermark image to a png image, i´ve been able to do it, but i want to take out the hardCoded size regulation for the rectangle of the waterMark, and make it always stay in the center of the image. How can i achieve this.
public Form1()
{
InitializeComponent();
picBox.Parent = this;
picBox.Dock = DockStyle.Fill;
picBox.SizeMode = PictureBoxSizeMode.Zoom;
Bitmap Jpg = new Bitmap(#"C:\Users\tferreira\Desktop\213123.PNG");
using (Bitmap Bmp = new Bitmap(#"C:\Users\tferreira\Desktop\logo.png"))
{
using (Bitmap WatermarkBmp = new Bitmap(Bmp, Bmp.Width / 1, Bmp.Height / 1))
{
picBox.Image = WatermarkImage(Jpg, WatermarkBmp, new Point(400, 100), 0.40F);
}
}
}
public Bitmap WatermarkImage(Bitmap ImageToWatermark, Bitmap Watermark, Point WatermarkPosition, float Opacity)
{
using (Graphics G = Graphics.FromImage(ImageToWatermark))
{
using (ImageAttributes IA = new ImageAttributes())
{
ColorMatrix CM = new ColorMatrix();
CM.Matrix33 = Opacity;
IA.SetColorMatrix(CM);
G.DrawImage(Watermark, new Rectangle(WatermarkPosition, Watermark.Size), 0, 0, Watermark.Width, Watermark.Height, GraphicsUnit.Pixel, IA);
}
}
return ImageToWatermark;
}
Right now the images are hardCoded but that will be taken out. If anyone can help me make this watermark allways stay centered i thank you.
I fixed this problem with this nice peace of code.
System.Drawing.Image img = System.Drawing.Image.FromFile(JpgFilePath);
Bitmap jpg = new Bitmap(img);
filePath = JpgFilePath;
int Width = jpg.Width;
int Height = jpg.Height;
jpg.SetResolution(300, 300);
WaterMarked = WatermarkImage(jpg, WaterMarkBit, new Point((Width - WaterMarkBit.Width) / 2, (Height - WaterMarkBit.Height) / 2), 0.4F);
WaterMarked.Save(filePath.Replace(".jpg", "") + ".tif", ImageFormat.Tiff);
filesJpgForTif.Add(JpgFilePath.Replace("jpg", "tif"));
Using the the sizes of the image and sizes of the watermark and divide it by 2 it makes the image always stay centered.

Unable to get correct Size of DrawnText using TextRenderer

Im drawing Text using the following code onto a Bitmap
GraphicsPath pth = new GraphicsPath();
var style = (int)myfont.Style;
pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, point, StringFormat.GenericTypographic);
p = new Pen(new SolidBrush(bc), 2f);
mygraphics.DrawPath(p, pth);
I'm using the TextRenderer to measure the size of the string..
int Width = TextRenderer.MeasureText(tcaption.Text, myfont).Width;
But this does not produce the correct size of the drawn string; there is around 20-30% difference from the actual size of the drawn string?
What im i doing wrong? Please advice.
UPDATE:
I want to draw a Text and an Image onto a Bitmap,so inorder to accommodate both i'm creating an Bitmap like this
intWidth = TextRenderer.MeasureText(tcaption.Text, cfont).Width + image.Width;
intHeight = TextRenderer.MeasureText(tcaption.Text, cfont).Height +image.Height;
tempimage= new Bitmap(intWidth, intHeight);
Then i create Graphics object from the Bitmap like this
using (Graphics newg = Graphics.FromImage(tempimage))
#Hans Passant
I have also tried the Graphics.MeasureString as an alternative to TextRenderer
Now i set the position of the text and image-I need to draw the image at the top left corner .. so
imageposy = 0;
imageposx = 10;
textposy = image.Height;
textposx = 0;
Then i draw the text like this
po=new Point(textposx, textposy);
newg.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath pth = new GraphicsPath();
var style = (int)myfont.Style;
pth.AddString(tcaption.Text, myfont.FontFamily, style, myfont.Size, po,
StringFormat.GenericTypographic);
newg.FillPath(new SolidBrush(fc), pth);
Now i draw the image like this
Rectangle nrect = new Rectangle(imageposx, imageposy, image.Width,
image.Height);
objGraphics = Graphics.FromImage(tempimage);
objGraphics.DrawImage(image, nrect);
As you have seen i need to add the offset 10 to imageposition x coordinate to correct the measurement issue.
Hope my update throws more light into the question... what im i doing wrong?
Please advice..
instead of using TextRenderer use GraphicsPath:
var path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
var area = Rectangle.Round(path.GetBounds());
Here is sample code that generates image with size of text:
private Image DrawText(String text, Font font, int size, Color textColor, Color backColor)
{
var path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int)font.Style, size, new Point(0, 0), StringFormat.GenericTypographic);
var area = Rectangle.Round(path.GetBounds());
Rectangle br = Rectangle.Round(path.GetBounds());
var img = new Bitmap(br.Width, br.Height);
var drawing = Graphics.FromImage(img);
drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
drawing.SmoothingMode = SmoothingMode.HighSpeed;
drawing.Clear(backColor);
drawing.TranslateTransform((img.Width - br.Width) / 2 - br.X, (img.Height - br.Height) / 2 - br.Y);
drawing.FillPath(Brushes.Black, path);
Brush textBrush = new SolidBrush(textColor);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
Here are sample results:

Adding text to a bitmap with canvas

I have some code that adds text to a bitmap. This works well unless the text is wider than the original image.
How do I go about making the image wide enough for my text to display correctly?
Here is my code:
private BitmapDescriptor GetCustomBitmapDescriptor(string text)
{
Bitmap baseBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon);
Bitmap bitmap = baseBitmap.Copy(Bitmap.Config.Argb8888, true);
Paint paint = new Paint(PaintFlags.AntiAlias);
Rect bounds = new Rect();
paint.GetTextBounds(text, 0, text.Length, bounds);
float bitmapMiddle = bitmap.Width / 2.0f;
Canvas canvas = new Canvas(bitmap);
canvas.DrawText(text, bitmapMiddle - (bounds.Right / 2.0f), bitmap.Height, paint);
BitmapDescriptor icon = BitmapDescriptorFactory.FromBitmap(bitmap);
return (icon);
}
Thanks in advance.
Just color code and it is working
private Bitmap GetCustomBitmapDescriptor(String text)
{
Bitmap baseBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launche);
Bitmap bitmap = baseBitmap.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
float bitmapMiddle = bitmap.getWidth() / 2.0f;
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.RED);
canvas.drawText(text, bitmapMiddle - (bounds.right / 2.0f), bitmap.getHeight(), paint);
return bitmap;
}

Fill / append rectangle on bottom of image c#

I need to fill an rectangle on bottom of my image, but not over the image, so it should be a kind of append a rectangle to bottom of image.
What I have for now:
private void DrawRectangle()
{
string imageFilePath = #"c:\Test.jpg";
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (Image img = Image.FromFile(imageFilePath))
{
SolidBrush brush = new SolidBrush(Color.Black);
int width = img.Width;
int height = img.Height - 350;
graphics.FillRectangle(brush, 0, height, width, 350);
}
}
bitmap.Save(#"c:\Test1.jpg");
}
But this is over the image.
Any idea's?
Thank you.
You need to know the dimensions of your original image in order to set the size of the new bitmap, which must be larger to accommodate the rectangle.
private void DrawRectangle()
{
string imageFilePath = #"c:\Test.jpg";
int rectHeight = 100;
using (Image img = Image.FromFile(imageFilePath)) // load original image
using (Bitmap bitmap = new Bitmap(img.Width, img.Height + rectHeight)) // create blank bitmap of desired size
using (Graphics graphics = Graphics.FromImage(bitmap))
{
// draw existing image onto new blank bitmap
graphics.DrawImage(img, 0, 0, img.Width, img.Height);
SolidBrush brush = new SolidBrush(Color.Black);
// draw your rectangle below the original image
graphics.FillRectangle(brush, 0, img.Height, img.Width, rectHeight);
bitmap.Save(#"c:\Test1.bmp");
}
}
take a look at the FillRectangle() method overloads.. it has one with the following definition: FillRectangle(Brush brush, int PositionX, int PositionY, int Width, int Height)
your problem most likely stems on using an improper overload for what you're doing.

Crop a bitmap and expand the size if necessary

I want to crop a bitmap with this function but the bitmap could be smaller then the crop area so I want to get the bitmap bigger in that case.
As example I have a bitmap which is 200x250 and if I use the CropBitmap method with 250x250 I get an out of memory error. It should return a bitmap with 250x250 where the missing left 50px are filled with white.
How can I achieve that?
public Bitmap CropBitmap(Bitmap bitmap, int cropX, int cropY, int cropWidth, int cropHeight)
{
var rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
if(bitmap.Width < cropWidth || bitmap.Height < cropHeight)
{
// what now?
}
return bitmap.Clone(rect, bitmap.PixelFormat);
}
Create a new Bitmap with the appropriate size. Then get a System.Drawing.Graphics and use it to create the white area and to insert the source image. Something like this:
if (bitmap.Width < cropWidth && bitmap.Height < cropHeight)
{
Bitmap newImage = new Bitmap(cropWidth, cropHeight, bitmap.PixelFormat);
using (Graphics g = Graphics.FromImage(newImage))
{
// fill target image with white color
g.FillRectangle(Brushes.White, 0, 0, cropWidth, cropHeight);
// place source image inside the target image
var dstX = cropWidth - bitmap.Width;
var dstY = cropHeight - bitmap.Height;
g.DrawImage(bitmap, dstX, dstY);
}
return newImage;
}
Note, that I replaced the || in the outer if expression with &&. To make it work with ||, you have to calculate the source region and use another overload of Graphics.DrawImage

Categories

Resources