html to jpg with c# - c#

I did some searching to try and generate jpg files from an html page and found one solution called IECapt or something similar that requires IE on the server to work...not what I want.
Here's what I'm looking to do: Generate a jpg image from an html page (the html page will just be text) and then put a watermark over the jpg.
Essentially, I'm creating a "sample" that my users can see which will just be an image created from html (again just straight text). That sample should have a watermark on it as mentioned above. Are there any libraries available to do this with c#? What I'd like is to pass in the url of my page that I want converted to a method and maybe the save path of the jpg, then have it work its magic, and convert that url to a jpg image, toss a watermark on it, then say hooray!
Edit 1
adding some code from the answer below..can't get my head around this:
InitialContainer c = new InitialContainer("<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>");
Bitmap m_Bitmap = new Bitmap(400, 700);
c.Paint(Graphics.FromImage(m_Bitmap));
m_Bitmap.Save(#"C:\test\Test.bmp");
Edit 2
this DOES work.
Bitmap m_Bitmap = new Bitmap(400, 600);
PointF point = new PointF(0,0);
HtmlRenderer.Render(Graphics.FromImage(m_Bitmap), "<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>",point, 500);
m_Bitmap.Save(#"C:\test\Test.bmp");

You can use this HtmlRenderer class.

I haven't tried this, but you can try using Control.DrawToBitmap().
To draw the watermark you can go like this:
Image img; //the html image.
Image watermark; //the watermark image.
Point location; //where to draw the watermark on the html image.
Graphics g = Graphics.FromImage(img);
g.DrawImage(watermark, location);

Related

Bitmap.Save(...) generates different image than actual

I've parsed an image with the following code
Bitmap img = (Bitmap)Bitmap.FromFile("file.jpg");
And after saved so
img.Save("file2.jpg");
I did not modify anything, but looking on diff of the both images, I see that the new one is different. How I can fix that? I want to keep the colors

How do I rotate a background watermark image to landscape with iTextSharp?

I'm building a PDF class with iTextSharp to handle my company's digital records. We use expensive paper that has columns and has our company's logo at the top. I've scanned a copy as a PDF and another as a JPG. I've read elsewhere that the IMG.Rotate() command should rotate the image. I've also read that Image.SetRotationDegrees(90) is supposed to work, but that line of code doesn't work for me. SetRotationDegrees isn't a method of Image. Here's what I've got so far:
string imageFilePath = _watermark;
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
//Rotate it for landscape
Image jpg.Rotate(); Image.GetInstance(System.Drawing.Image.FromStream(fs), ImageFormat.Jpeg);
//Scale image
jpg.ScalePercent(35f);
//Set position
jpg.ScaleToFit(770, 3000);
jpg.WidthPercentage = 100;
jpg.Rotate();
//Allignment
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
...
PDFReport.Add(jpg);
`
Anyone have any ideas? The image is being placed on the page but it places it portrait instead of landscape.

C# selenium webdriver - How to save a specific image regardless of location

I've seen a ton of examples how to save an image based on saving a screenshot.
This has a fundamental flaw. The take screenshot only takes whats visable on the page at the time. So if I have an image at the bottom of the page, and I want to save it based on the location of the elements that I found, either one or two problems occur.
If I save the screenshot and then try to save by the location, the screen shot ends at point 1200 but the image is located at 3000. If i focus on the image and then take a screen shot, the image is is now on the screen shot, however, the location doesn't work. It doesn't work because I still have a 1200px height image with a location of 3000.
How can I simply say, I have an image at 3000x 3014y and I just want to save it?
You can basically get base64 string of the image and save it to file.
var base64string = driver.ExecuteScript(#"
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var img = document.getElementsByTagName('img')[0];
c.height=img.height;
c.width=img.width;
ctx.drawImage(img, 0, 0,img.width, img.height);
var base64String = c.toDataURL();
return base64String;
") as string;
var base64 = base64string.Split(',').Last();
using (var stream = new MemoryStream(Convert.FromBase64String(base64)))
{
using (var bitmap = new Bitmap(stream))
{
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid()}.jpg");
bitmap.Save(filepath, ImageFormat.Jpeg);
}
}
I've resolved it. Chrome vs IE vs Firefox take screen shots in different manners. Firefox will stitch the screenshot together, chrome only takes whats visible and IE will shrink everything to try to fit on one page.
If you need to do a screenshot of something that is not on the base page i'd suggest firefox

Dynamically create a picture with some texts and background image as PNG

I have the following:
A headline (text)
A tagline (text)
A background image (url to this image)
I need to set these dynamically together in an image, and then store it as a PNG file. All of this happens in a web service which sends some kind of stream back.
I would love to be able to style this with CSS, as it would be nice to add some CSS effects to the picture. If that is not possible, it is fine as well.
So far I've thought about using the Bitmap class, and then insert my things dynamically. This is definetely one way to go, but I would really prefer some more design-friendly way like CSS.
What is the prefered way to do this? Any experiences?
Check out System.Drawing.Graphics.
Bitmap bmp = new Bitmap(500, 500);
using (Graphics g = Graphics.FromImage(bmp)) {
g.Clear(Color.Black);
// ...
}
You can then call Image.Save (bmp.Save(...)) to save the image to disk or even to a MemoryStream.
The answer from Angelo Geels will let you create a dynamic bitmap in your code. If you need more flexibility then you might want to use the SVG format instead, and possibly render it as a PNG file if you absolutely need that format.
SVG is an XML based image format where you define a viewport and shapes that should be drawn into it. It is vector based so the images can be scaled up and down to any size.
It should be quite easy to write an ASP.Net Http Handler that outputs an SVG xml file with some dynamic content based on user input. An even easier option would be to serve static SVG files that your designers create, but then you might as well serve static PNG files.
If you want to use the SVG approach for its flexibility but still need to output PNG's then you can use the SVG.Net library to read the SVG file and then use the SvgDocument.Draw method to draw the image to a Bitmap object that can then be written out as a PNG file.

How to zoom an image in&out in C#?

I want to implement zoom for an image. I don't want to resize the PictureBox, but the image itself.
How do I do this?
One solution is:
Create new image of the desired size (for example 200% or 50% of original image size)
Draw original image to new image using Graphics.DrawImage(Image, Rectangle);, which draws the given image to the new image at the given position with the given size
Set new image as source for the PictureBox
Another way is to simple create a new bitmap instance like that:
Size newSize = new Size((int)(originalBitmap.Width * zoomFactor), (int)(originalBitmap.Height * zoomFactor));
Bitmap bmp = new Bitmap(originalBitmap, newSize);
I used a web browser to achieve this.
//loads the image
myWebBrowser.Navigate(#"C:\myimage.png");
From there I used SendKeys to zoom in and out
myWebBrowser.Select(); //Selects browser.
SendKeys.Send("^{+}"); //Sends the control + key combo, causing the browser to zoom in. Replace the "+" with a "-" to zoom out.
It's a bit of a weird method, but it worked really well for me. I hope you find this helpful!

Categories

Resources