Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a WPF application which has a picture box with a graphic in it, I need a way to be able to change this graphic in a simple straightforward manner (like replacing an image file in the programs install directory).
I'm not sure if this is what you want, but...
You could do something like this:
Source="pack://siteoforigin:,,,/Images/someimage.png"
and use images off of your bin/app folder. Take a look at this link for more info...
What is application's site of origin and when to use it
Define a function that loads an image from an external image file
public static ImageSource LoadImage(string fileName)
{
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri("file:///" + fileName.Replace("\\", "/"));
bitmap.EndInit();
return bitmap;
}
You can then assign an image controls source to this functions return value.
someImageControl.Source = LoadImage(#"d:\\images\\image.png");
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How to convert a dynamic link which is a html web page into an Image format. Remember the link is dynamic which contains html content in string format. I have tried a lot of ways like reading the html content using converting to base64 first then visa versa.
var htmlToImageConv = new HtmlToImageConverter();
byte[] jpegBytes = htmlToImageConv.GenerateImage(html, ImageFormat.Jpeg); System.Drawing.Image image; using (System.IO.MemoryStream ms = new System.IO.MemoryStream(strOg))
{
image = System.Drawing.Image.FromStream(ms); string path = Server.MapPath("~/images/");
}
I have tried this code in c# for converting html webpage to image.
You can use a headless browser to render the html and then take a snapshot.
Have a look at PuppeteerSHarp: https://github.com/kblok/puppeteer-sharp
You could use Selenium to render the page and save a screenshot as a png image.
Add the following packages to your project:
Selenium.WebDriver
Selenium.Chrome.WebDriver
Use the following code to save a screenshot:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://google.com");
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile("screenshot.png");
}
}
}
That what you need is a conversation from a html containing string to an image, which is already discussed in the answers of this Question.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got success to post photos to Twitter. But how do i post videos to Twitter using my C# application?. For posting pictures i used Tweetsharp. Can we use Tweetsharp's SendTweetWithMediaOptions to share video? if yes How?
This is sample i used to share picture to twitter
Bitmap img = new Bitmap(Server.MapPath(#"~/Images/Special/" + Convert.ToInt32(offerId) + "/" + specialOffer.Picture));
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
Dictionary<string, Stream> images = new Dictionary<string, Stream> { { "mypicture", ms } };
var tweet = tweetservice.SendTweetWithMedia(new SendTweetWithMediaOptions { Status = urlTextToShare, Images = images });
and what i need is something like below image
You cannot upload videos to Twitter. If you read their documentation, it says:
Supported image formats are PNG, JPG and GIF, including animated GIFs of up to 3MB
You choices are either to upload an animated GIF or include a URL to a video (such as YouTube / Vimeo)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Basically, I want to rehash VisualCube, written previously in PHP.
I've looked into GDI+, tried to find books that dealt with C#, graphics, etc.
Everything somewhat relevant is aimed at only WinForms or WPF, while I'd ideally want to create a WebAPI or WCF service that serves up the images.
What technologies can I use for this? If GDI+, can someone provide me a usage in WebAPI/WCF?
I'd be accessing the WebAPI/WCF through MVC4.
Hanselman has an example with explanation:
http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx
public ActionResult DynamicImage()
{
using (Bitmap image = new Bitmap(200, 200))
{
using (Graphics g = Graphics.FromImage(image))
{
string text = "Hello World!";
Font drawFont = new Font("Arial", 10);
SolidBrush drawBrush = new SolidBrush(Color.Black);
PointF stringPonit = new PointF(0, 0);
g.DrawString(text, drawFont, drawBrush, stringPonit);
}
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return File(ms.ToArray(), "image/png");
}
}
You could check here: using-wpf-to-generate-a-bitmap
http://social.msdn.microsoft.com/Forums/vstudio/en-US/2857e468-c32a-45dd-be02-cbd920259950/using-wpf-to-generate-a-bitmap-or-other-image-on-an-aspnet-web-application-form
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Looking at other posts for this could not find an adequate solution that for my needs. Trying to just get the first page of a pdf document as a thumbnail. This is to be run as a server application so would not want to write out a pdf document to file to then call a third application that reads the pdf to generate the image on disk.
doc = new PDFdocument("some.pdf");
page = doc.page(1);
Image image = page.image;
Thanks.
Matthew Ephraim released an open source wrapper for Ghostscript that sounds like it does what you want and is in C#.
Link to Source Code: https://github.com/mephraim/ghostscriptsharp
Link to Blog Posting: http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/
You can make a simple call to the GeneratePageThumb method to generate a thumbnail (or use GeneratePageThumbs with a start and end page number to generate thumbnails for multiple seperate pages, with each page being a seperate output file), default file format is jpeg but you can change it, and many other options, by using the alternate GenerateOutput method call and specify options such as file format, page size, etc...
I think that Windows API Code pack for Microsoft .NET framework might do the trick easiest. What it can is to generate the same thumbnail that Windows Explorer does (and that is first page), and you can chose several sizes, they go up to 1024x1024, so it should be enough. It is quite simple, just create ShellObject.FromParsingName(filepath) and find its Thumbnail subclass.
The problem might be what your server is. This works on Windows 7, Windows Vista and I guess Windows Server 2008. Also, Windows Explorer must be able to show thumbnails on that machine. The easiest way to insure that is to install Adobe Reader. If all of this is not a problem, I think that this is the most elegant way.
UPDATE: Adobe Reader has dropped support for thumbnails in the recent versions so its legacy versions must be used.
UPDATE2: According to comment from Roberto, you can still use latest version of Adobe Reader if you turn on thumbnails option in Edit - Preferences - General.
Download PDFLibNet and use the following code
public void ConvertPDFtoJPG(string filename, String dirOut)
{
PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
_pdfDoc.LoadPDF(filename);
for (int i = 0; i < _pdfDoc.PageCount; i++)
{
Image img = RenderPage(_pdfDoc, i);
img.Save(Path.Combine(dirOut, string.Format("{0}{1}.jpg", i,DateTime.Now.ToString("mmss"))));
}
_pdfDoc.Dispose();
return;
}
public Image RenderPage(PDFLibNet.PDFWrapper doc, int page)
{
doc.CurrentPage = page + 1;
doc.CurrentX = 0;
doc.CurrentY = 0;
doc.RenderPage(IntPtr.Zero);
// create an image to draw the page into
var buffer = new Bitmap(doc.PageWidth, doc.PageHeight);
doc.ClientBounds = new Rectangle(0, 0, doc.PageWidth, doc.PageHeight);
using (var g = Graphics.FromImage(buffer))
{
var hdc = g.GetHdc();
try
{
doc.DrawPageHDC(hdc);
}
finally
{
g.ReleaseHdc();
}
}
return buffer;
}
I used to do this kind of stuff with imagemagick (Convert) long ago.
There is a .Net Wrapper for that, maybe it's worth checking out :
http://imagemagick.codeplex.com/releases/view/30302
http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx
This works very well. The only dependencies are GhostScript's gsdll32.dll (you need to download GhostScript separately to get this, but there is no need to have GhostScript installed in your production environment), and PDFSharp.dll which is included in the project.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I have found various code and libraries for editing Exif.
But they are only lossless when the image width and height is multiple of 16.
I am looking for a library (or even a way to do it myself) to edit just the Exif portion in a JPEG file (or add Exif data if it doesn't exist yet), leaving the other data unmodified. Isn't that possible?
So far I could only locate the Exif portion (starts with 0xFFE1) but I don't understand how to read the data.
Here are the specifications for the Exif interchange format, if you plan to code your own library for editing tags.
http://www.exif.org/specifications.html
Here's a library written in Perl that meets your needs that you may be able to learn from:
http://www.sno.phy.queensu.ca/~phil/exiftool/
Here's a decent .NET library for Exif evaluation from The Code Project:
http://www.codeproject.com/KB/graphics/exiftagcol.aspx
You can do this without any external lib:
// Create image.
Image image1 = Image.FromFile("c:\\Photo1.jpg");
// Get a PropertyItem from image1. Because PropertyItem does not
// have public constructor, you first need to get existing PropertyItem
PropertyItem propItem = image1.GetPropertyItem(20624);
// Change the ID of the PropertyItem.
propItem.Id = 20625;
// Set the new PropertyItem for image1.
image1.SetPropertyItem(propItem);
// Save the image.
image1.Save("c:\\Photo1.jpg", ImageFormat.Jpg);
List of all possible PropertyItem ids (including exif) you can found here.
Update: Agreed, this method will re-encode image on save. But I have remembered another method, in WinXP SP2 and later there is new imaging components added - WIC, and you can use them to lossless write metadate - How-to: Re-encode a JPEG Image with Metadata.
exiv2net library (a .NET wrapper on top of exiv2) may be what you're looking for.
I wrote a small test where I compress one file many times to see the quality degradation and you can see it in the third-fourth compression, which is very bad.
But luckily, if you always use same QualityLevel with JpegBitmapEncoder there is no degradation.
In this example I rewrite keywords 100x in metadata and the quality seems not to change.
private void LosslessJpegTest() {
var original = "d:\\!test\\TestInTest\\20150205_123011.jpg";
var copy = original;
const BitmapCreateOptions createOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
for (int i = 0; i < 100; i++) {
using (Stream originalFileStream = File.Open(copy, FileMode.Open, FileAccess.Read)) {
BitmapDecoder decoder = BitmapDecoder.Create(originalFileStream, createOptions, BitmapCacheOption.None);
if (decoder.CodecInfo == null || !decoder.CodecInfo.FileExtensions.Contains("jpg") || decoder.Frames[0] == null)
continue;
BitmapMetadata metadata = decoder.Frames[0].Metadata == null
? new BitmapMetadata("jpg")
: decoder.Frames[0].Metadata.Clone() as BitmapMetadata;
if (metadata == null) continue;
var keywords = metadata.Keywords == null ? new List<string>() : new List<string>(metadata.Keywords);
keywords.Add($"Keyword {i:000}");
metadata.Keywords = new ReadOnlyCollection<string>(keywords);
JpegBitmapEncoder encoder = new JpegBitmapEncoder {QualityLevel = 80};
encoder.Frames.Add(BitmapFrame.Create(decoder.Frames[0], decoder.Frames[0].Thumbnail, metadata,
decoder.Frames[0].ColorContexts));
copy = original.Replace(".", $"_{i:000}.");
using (Stream newFileStream = File.Open(copy, FileMode.Create, FileAccess.ReadWrite)) {
encoder.Save(newFileStream);
}
}
}
}