I want to take a screen shot of full code from visual studio single file.
is it possible?
if yes, how?
print to something like a PDF writer? or 'Microsoft Office Document Image Writer'?
Think that would be the way to go.
You can try this
File
Print
Select Printer Name as Microsoft XPS Document Writer
Press OK
To support colors, you can paste the code into an enormous RichTextBox (or regular-sized with NumericUpDowns to control width and height) and have a button that will do:
var bitmap = new Bitmap(this.richTextBox1.Width, this.richTextBox1.Height);
this.richTextBox1.DrawToBitmap(bitmap, new Rectangle(Point.Empty, bitmap.Size));
bitmap.Save("code.bmp");
Legacy (doesn't support colors):
var allCode = "...copy all of the code into here";
var font = new Font("Arial", 13);
SizeF size;
using (var g = Graphics.FromImage(new Bitmap(1, 1)))
{
size = g.MeasureString(allCode, font);
}
var bitmap = new Bitmap((int)size.Width + 20, (int)size.Height + 20);
using (var g = Graphics.FromImage(bitmap))
{
g.DrawString(allCode, font, Brushes.Black, 10, 10);
}
bitmap.Save("code.bmp");
You can use this:
Bitmap BmpScreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics ScreenShot = Graphics.FromImage(BmpScreen);
ScreenShot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
string file = "C:\\test.jpg";
BmpScreen.Save(file, System.Drawing.Imaging.ImageFormat.Png);
is necessary: using System.Drawing;
Related
I am currently making an MVC project where the User will receive a Certificate which is displayed on the page. The Certificate is an image and then Text is used to overlap the image where the Users name and other information will be printed, using css to format the text to the correct part of the image.
As the image and text are technically still separate, saving the image does not save the text on top so what you save is just the template of a certificate and no text.
Is there a way to save both image and text as one, as if the text was pressed onto the image and was the same object? If so, I would appreciate being pointed in the right direction to know how to do this. Any ideas or code to save an image and text as one it would be very helpful.
Thanks.
May this help you
private void makeCertificate(string name, string id, string otherDetails) //You can pass any other details as well
{
System.Drawing.Image PrePrintedCertificate;
name = name.ToUpper();
string PrePrintedCertificateName = "Certificate.jpg"; //Assuming Certificate JPG File is in the bin folder
using (FileStream stream = new FileStream(PrePrintedCertificateName, FileMode.Open, FileAccess.Read))
{
PrePrintedCertificate = System.Drawing.Image.FromStream(stream);
}
RectangleF rectf4Name = new RectangleF(655, 460, 535, 90); //rectf for Name
RectangleF rectf4ID = new RectangleF(655, 560, 400, 40);
System.Drawing.Rectangle rect;
Bitmap picEdit = new Bitmap(PrePrintedCertificate, new System.Drawing.Size(1241, 1756));
using (Graphics g = Graphics.FromImage(picEdit))
{
//g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 2), 662, 530, 90, 40);
//I have used upper line to see where is my RectangleF creating on the image
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
StringFormat sf1 = new StringFormat();
sf1.Alignment = StringAlignment.Near;
//g.DrawImage(codeImage, rect); //If you wanted to draw another image on the certificate image
g.DrawString(name, new System.Drawing.Font("Thaoma", 26, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, rectf4Name, sf);
g.DrawString(Track.Text, new System.Drawing.Font("Thaoma", 14, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, rectf4ID, sf1);
}
try
{
if (File.Exists(id + ".jpg"))
File.Delete(id + ".jpg");
picEdit.Save(id + ".jpg", ImageFormat.Jpeg);
picEdit.Dispose();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Things to note here is that this image is a A4 size paper image with portrait orientation. You probably need to change Bitmap picEdit = new Bitmap(PrePrintedCertificate, new System.Drawing.Size(1241, 1756)); to Bitmap picEdit = new Bitmap(PrePrintedCertificate, new System.Drawing.Size(1756,1241));
Another thing is the names and other details has been printed on the image with random place but you can see where exactly you wish to print the details.
You can look where it will get printed using g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 2), 662, 530, 90, 40); the Parameters you gonna pass here will be same as the RectangleF Parameters.
Save the graphic as an SVG and add the text using a text object.
You can also use CSS within the SVG to style the text.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<text x="20" y="40">Example SVG text 1</text>
</svg>
I currently have an image which gets displayed to the user, I'm trying to add dynamic text to this image based on two parameters passed in.
The issue I have is when I step through the code it all seems to be working correctly, however when I see the image on the screen after the below code has run it doesn't have the text on it.
Below is my current set up of code:
public ActionResult GenerateImage(string savingAmount, string savingDest)
{
// Hardcoding values for testing purposes.
savingAmount = "25,000.00";
savingDest = "Canada";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
Image imgBackground = Image.FromFile(Server.MapPath("~/assets/img/fb-share.jpg"));
int phWidth = imgBackground.Width; int phHeight = imgBackground.Height;
Bitmap bmBackground = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
bmBackground.SetResolution(72, 72);
Graphics grBackground = Graphics.FromImage(bmBackground);
Bitmap bmWatermark;
Graphics grWatermark;
bmWatermark = new Bitmap(bmBackground);
bmWatermark.SetResolution(imgBackground.HorizontalResolution, imgBackground.VerticalResolution);
grWatermark = Graphics.FromImage(bmWatermark);
grBackground.SmoothingMode = SmoothingMode.AntiAlias;
// Now add the dynamic text to image
using (Graphics graphics = Graphics.FromImage(imgBackground))
{
using (Font arialFont = new Font("Arial", 10))
{
grWatermark.DrawString(savingAmount, arialFont, Brushes.White, firstLocation);
grWatermark.DrawString(savingDest, arialFont, Brushes.White, secondLocation);
}
}
imgBackground.Save(Response.OutputStream, ImageFormat.Png);
Response.ContentType = "image/png";
Response.Flush();
Response.End();
return null;
}
As mentioned after this code has run, I then see the image in the browser however text is not displayed on the image, can anyone see / suggest what maybe causing this issue?
I feel like there are way to many images in that code for what you are describing as the intent of the code. What you want should reduce to this:
Load Image
Create Graphics on that Image
Draw into the Graphics and close
Output image to client
In the code sample you provided you are opening the Graphics on imgBackground then drawing into the grWatermark graphics which is opened earlier on against an image you never touch again.
public ActionResult GenerateImage(string savingAmount, string savingDest)
{
// Hardcoding values for testing purposes.
savingAmount = "25,000.00";
savingDest = "Canada";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
Image imgBackground = Image.FromFile(Server.MapPath("~/assets/img/fb-share.jpg"));
using (Graphics graphics = Graphics.FromImage(imgBackground))
{
using (Font arialFont = new Font("Arial", 10))
{
graphics.DrawString(savingAmount, arialFont, Brushes.White, firstLocation);
graphics.DrawString(savingDest, arialFont, Brushes.White, secondLocation);
}
}
imgBackground.Save(Response.OutputStream, ImageFormat.Png);
Response.ContentType = "image/png";
Response.Flush();
Response.End();
return null;
}
I want to combine two image but I cant success . Help me please.
When try the combine PictureBox show only first image but there is not second image , when I remove first image I can see second image.
Also I tried setting first image and draw the text on image that's also not working. Please help.
Image myimg = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap image = new Bitmap(myimg.Width + 20, myimg.Height + 50);
pictureBox1.DrawToBitmap(image, new Rectangle(0, 0, myimg.Width + 20, myimg.Height + 50));
Bitmap bmp = new Bitmap(myimg.Width + 20, myimg.Height);
Bitmap bmp2 = new Bitmap(myimg.Width + 20, 20);
Graphics Cizgi2 = Graphics.FromImage(bmp2);
Graphics Cizgi = Graphics.FromImage(bmp);
Cizgi.DrawImage(myimg, 0, 0);
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
Cizgi2.DrawString(textBox1.Text, fonts, Brushes.Black, 5, myimg.Height + 10);
Graphics g = Graphics.FromImage(image);
g.DrawImage(bmp, new Point(10, 0));
g.DrawImage(bmp2, new Point(0, bmp.Height + 10));
I want to image seems like first but i cant make
It looks like you are trying to concatenate two images vertically? It's pretty simple actually, you can look here (C# image concatenation), but I've also modified it for your needs. I think this should work:
float drawBorderX = 5;
float drawBorderY = 5;
//Set up our two images
Bitmap barCode = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap text = new Bitmap(barCode.Width, 50);
Graphics textGraphics = Graphics.FromImage(text);
//Draw the text to the bottom image.
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
textGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, text.Width, text.Height));
textGraphics.DrawString(textBox1.Text, fonts, Brushes.Black, drawBorderX, drawBorderY);
//Vertically concatenate the two images.
Bitmap resultImage = new Bitmap(Math.Max(barCode.Width, text.Width), barCode.Height + text.Height);
Graphics g = Graphics.FromImage(resultImage);
g.DrawImage(barCode, 0, 0);
g.DrawImage(text, 0, barCode.Height);
Edit: Note that resultImage will contain the image you want, so you can set your PictureBox to be that image at the end.
I perform a screen capture and get an image with a text in it. Let consider the text in the image to read - 'Hello'.
Now, I would like to 'create' an image from text 'Hello' which has all the same properties (font style, size, pixel format..) as the text - Hello from my captured image.
I'm using the following code to convert the string - Hello to an image.
string str = "Hello";
Bitmap bmp = new Bitmap(74, 16, PixelFormat.Format32bppArgb);
using (Graphics gfx = Graphics.FromImage((Image)bmp))
{
Font font = new Font("Tahoma", 11, FontStyle.Regular, GraphicsUnit.Pixel);
gfx.FillRectangle(Brushes.Transparent, new RectangleF(0, 0, bmp.Width, bmp.Height));
gfx.FillRectangle(Brushes.Black, 0, 0, 74, 16);
gfx.DrawString(str, font, new SolidBrush(Color.White), 1, 1);
bmp.Save(#"C:\temp\" + str + ".bmp", ImageFormat.Bmp);
}
The two images (one from screen capture, and second from creating) are not the same.
How do I create an image with text which would match exactly to the image with text from the screen capture ?
Here is the code I use to screen capture from the third party application....
Rectangle rect = new Rectangle(194, 41, 74, 16);
using (Bitmap bmpScreenShot = new Bitmap(rect.Width, rect.Height))
{
using (Graphics gfxScreenShot = Graphics.FromImage(bmpScreenShot))
{
gfxScreenShot.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmpScreenShot.Size, CopyPixelOperation.SourceCopy);
gfxScreenShot.Dispose();
MemoryStream imageStream = new MemoryStream();
bmpScreenShot.Save(imageStream, ImageFormat.Bmp);
bmpScreenShot.Save(#"c:\temp\pic1_0.bmp");
}
}
I assume you're trying to implement an OCR. I've never tried anything like it, and can imagine it get's quite complicated. You might want to check out other (open-source) OCRs, like:
OCR in the Cloud, by Microsoft
Tessnet2
I need to add text to an image file. I need to read one image file (jpg,png,gif) and I need add one line text to it.
Well in GDI+ you would read in the file using a Image class and then use the Graphics class to add text to it. Something like:
Image image = Image.FromFile(#"c:\somepic.gif"); //or .jpg, etc...
Graphics graphics = Graphics.FromImage(image);
graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0);
If you want to save the file over the old one, the code has to change a bit as the Image.FromFile() method locks the file until it's disposed. The following is what I came up with:
FileStream fs = new FileStream(#"c:\somepic.gif", FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(fs);
fs.Close();
Bitmap b = new Bitmap(image);
Graphics graphics = Graphics.FromImage(b);
graphics.DrawString("Hello", this.Font, Brushes.Black, 0, 0);
b.Save(#"c:\somepic.gif", image.RawFormat);
image.Dispose();
b.Dispose();
I would test this quite thoroughly though :)
Specifically for gifs to have a gif result, you should write on each frame like the following:
string originalImgPath = #"C:\test.gif";
Image IMG = Image.FromFile(originalImgPath);
FrameDimension dimension = new FrameDimension(IMG.FrameDimensionsList[0]);
int frameCount = IMG.GetFrameCount(dimension);
int Length = frameCount;
GifBitmapEncoder gEnc = new GifBitmapEncoder();
for (int i = 0; i < Length; i++)
{
// Get each frame
IMG.SelectActiveFrame(dimension, i);
var aFrame = new Bitmap(IMG);
// write one the selected frame
Graphics graphics = Graphics.FromImage(aFrame);
graphics.DrawString("Hello", new Font("Arial", 24, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, 50, 50);
var bmp = aFrame.GetHbitmap();
var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
// merge frames
gEnc.Frames.Add(BitmapFrame.Create(src));
}
string saveImgFile = #"C:\modified_test.gif"
using (FileStream fs2 = new FileStream(saveImgFile, FileMode.Create))
{
gEnc.Save(fs2);
}
I should have mentioned that getting gif frames from this post.
You can do this by using the Graphics object in C#. You can get a Graphics object from the picture ( image.CreateGraphics() - or something like this as I remember ) and the use some of the built in methods for adding text to it like : Graphycs.DrawString() or other related methods.