I would like to generate an image from text that I get from a database. I know how to do that no problem, however where I need help is how to dynamically shrink or grow the bounds of the image based on how much text there is. I will not know how much text there will be in the database column. Is there a way to wrap text in a generated image somehow?
Thanks!
If you know how big you want the rectangle to be you can something like the following.
Bitmap bmp = new Bitmap(1000,1000);
using (Graphics g = Graphics.FromImage(bmp))
{
string s = "This string will be wrapped in the output rectangle";
RectangleF rectf = new RectangleF (10, 100, 200, 200);
g.DrawString(s, DefaultFont, Brushes.Red, rectf);
this.BackgroundImage = bmp; //For testing purposes set the form's background to the image
}
Related
What I'm trying to do:
Since in my bitmaps there are some unwanted white edges around the picture that result from anti-aliasing as pointed out from another user from stackoverflow.
I'm trying to convert an image that's inputted into a bitmap, convert bitmap into a Graphics object so that I can set the Smooth Mode to none, and then finally convert that Graphics object to a bitmap so that it can be copied by the user after setting it to the clipboard. I'm not sure if this is a good way of getting rid anti-aliasing in bitmaps but I'm definitely interested in improvements and suggestions.
The issue I'm facing:
The result of the image after is completely blank and does not contain any of the pixels that are previously found in the original bitmap. Here's the result:
This issue applies to all pictures no matter what their format is.
My code:
public PicGen(PictureBox pictureBox)
{
Clipboard.Clear();
Bitmap firstImage = new(pictureBox.Image, pictureBox.Width, pictureBox.Height);
RectangleF cloneRect = new RectangleF(0, 0, firstImage.Width, firstImage.Height);
System.Drawing.Imaging.PixelFormat format = firstImage.PixelFormat;
Bitmap cloneBitmap = firstImage.Clone(cloneRect, format);
Graphics AntiARemover = Graphics.FromImage(cloneBitmap);
AntiARemover.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
Bitmap finalImg = new(52, 52, AntiARemover);
Clipboard.SetImage(finalImg);
Color backColorBottom = firstImage.GetPixel(0, 0);
firstImage.ReplaceColor(backColorBottom, Color.FromArgb(54, 57, 63));
Bitmap finalImg = new(52, 52, AntiARemover);
From the documentation for this bitmap constructor:
The new Bitmap that this method creates takes its horizontal and vertical resolution from the DpiX and DpiY properties of g, respectively.
If you want create a new image with the content from another you need to call one of the DrawImage methods. You should also dispose your graphics object, and any temporary bitmaps you may use.
using var finalImg = new Bitmap(52,52);
using var graphics = Graphics.FromImage(finalImg);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
graphics.DrawImage(cloneBitmap)
However, edge artifacts typically occur when combining two images using an alpha channel, see Premultiplied alpha. In your example I can only see one input image, so I'm really not sure what it is you are actually trying to do. If you need to convert to premultiplied alpha you can use the following code to convert the color for each pixel
premultiplied.R = (byte)(straight.R * straight.A / 255);
premultiplied.G = (byte)(straight.G * straight.A / 255);
premultiplied.B = (byte)(straight.B * straight.A / 255);
premultiplied.A = straight.A;
Heey. I wanna write a program for a report system. I wanna Show up the result in a .jpg image.
I have many variable for name, age, sex etc etc etc.
Here comes the question..
Why not do in Word?
Ya. This result should be in .jpg not .docx for because the image will be post on websites.
How should "put" variables onto image?
Heres a example for what i want: http://prntscr.com/s8bgze
You can do it like this:
// load your photo
var photo = new Bitmap("photo.jpg");
// create an image of the desired size
var bitmap = new Bitmap(200, 300);
using (var graphics = Graphics.FromImage(bitmap))
{
// specify the desired quality of the render and text, if you wish
//graphics.CompositingQuality = CompositingQuality.HighQuality;
//graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
// set background color
graphics.Clear(Color.White);
// place photo on image in desired location
graphics.DrawImageUnscaled(photo, 0, 0);
using (var font = new Font("Arial", 12))
{
// draw some text on image
graphics.DrawString("Name: X Y", font, Brushes.Black, 0, 200);
graphics.DrawString("Age: 19", font, Brushes.Black, 0, 230);
// etc
}
}
// save image to file or stream
bitmap.Save("edited.phg", ImageFormat.Png);
Instead of Graphics.DrawString method you can use TextRenderer.DrawText (they have small differences in the drawing of the text).
Also, do not use jpg format for images containing text. Instead, take png.
I'm using C# to write a text in a certain format. My problem is that when I edit font size both width and height are changing while I just want to change the font height.
My code:
using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (System.Drawing.Font romanfont = new System.Drawing.Font("Times New Roman",11, FontStyle.Bold))
//using (System.Drawing.Font romanfont = new System.Drawing.Font("Times New Roman", 11, FontStyle.Bold))
{
SolidBrush transBrush = new SolidBrush(Color.FromArgb(65, 79, 79));
StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
graphics.DrawString(firstname, romanfont, transBrush, firstnameLocation, format);
graphics.DrawString(secondname, romanfont, transBrush, secondnameLocation, format);
graphics.DrawString(finalfirstadd, romanfont, transBrush, firstaddresslocation, format);
graphics.DrawString(finalsecondadd, romanfont, transBrush, secondaddresslocation, format);
}
}
You can achieve this effect by setting a transform on the Graphics object.
For example, if you want to make the text twice as tall but still the same width, you can do this:
graphics.scaleTransform(1, 2);
You would put this anywhere above the place where you draw your strings. Note that this change will make everything twice as tall, so you may need to adjust your positions and sizes of your rectangles (such as firstnameLocation; in this case you'd probably want to divide the top and height of the rectangle by 2.)
I've been asked to generate placeholder graphics for some items in a database but with one caveat - the font size needs to be dynamic so that the text fills the entire graphic.
The first iteration of the code works just fine, but the client wants to the font to be bigger so that the graphics are easier to read. Here's a snippet of the part which generates the text:
using (Graphics Graphic = Graphics.FromImage(Img))
{
// Add Some Padding
Width = Width - 20;
Height = Height - 20;
// Generate The Text
Font GraphicFont = new Font("Arial", 26, FontStyle.Bold);
RectangleF RectF = new RectangleF(10, 10, (float)Width, (float)Height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Near;
Graphic.DrawString(Title, GraphicFont, Brushes.Black, RectF, sf);
}
Is there I can detect text overflow in my RectangleF, or can I detect the size of my DrawString before I apply it to the graphic, or is there some other magic I can do to get the text to fill the graphic?
I would like the text to wordwrap and the font to be the same size for all the words, so it doesn't necessarily need to fill the graphic entirely - hope this all makes sense.
Use Graphics.MeasureString (documentation: http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx)
If you want to size text to fill a region, you can do binary search on different sizes to determine the largest size that still fits in your target region, then use that size to actually draw the string.
am using c#
am having a bitmap image like below
i want create a repeated image like below in horizontal position to get repeted continous image for some given width. i meant i like to draw repeated image like below from the above single bitmap (In simple words,in html we can have a image and set repeat X to get the repeated image.like that) how i can do this in c#.
so that i can draw a new bitmap in my application. How to do this.?
//x- integer value represents no. of times images to repeated horizontally
var destImage = new Bitmap(sourceImage.Width * x, sourceImage.Height, PixelFormat.Format32bppArgb);
using (TextureBrush brush = new TextureBrush(sourceImage, WrapMode.Tile))
using (Graphics g = Graphics.FromImage(destImage))
{
// Do your drawing here
g.FillRectangle(brush, 0, 0, destImage.Width, destImage.Height);
destImage.Save(#"C:\sourceImage.png", ImageFormat.Png);
//mention path of image to save, if needed
}
You can do it like this:
Bitmap myImage = new Bitmap(50, 50); //assuming you want you image to be 50,50
Bitmap originalImage = new Bitmap("myPngSource.png"); //original image to copy
using (Graphics g = Graphics.FromImage(myImage))
{
g.DrawImage(originalImage, new Rectangle(0, 0, originalImage.Width, originalImage.Height));
}
MemoryStream ms = new MemoryStream();
myImage.Save(ms, ImageFormat.Png);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
MyImageControl.Source = bi;
Or something like that, this is untested, and I just ripped it out of a little utility app I made a while ago. I hope it helps... You just need to change the width of the final image and do a loop over the g.DrawImage call incrementing the second parameter by the width of the originalImage. (i.e. if you want 5 repeats, do a for loop 5 times)
HTH
--Mark
you don't need to create other bitmaps. it's a matter of drawing bitmap. in the place you darw the bitmap use
drawImage method few times and increment the X position of the bitmap by its width. say 16 is the width of your image. make sure that bitmap has been initialized.
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bmp,x,y);
e.Graphics.DrawImage(bmp,x+16,y);
e.Graphics.DrawImage(bmp,x+32,y);
}