How to create image using barcode - c#

I would like to create Image from Barcode128 using iText7.
In iText5 it works well like as follows:
var barcode = new Barcode128();
...
System.Drawing.Image image = barcode.CreateDrawingImage(Color.Black, Color.White);
However there is no such method CreateDrawingImage in iText7. How do I achieve the same functionality in iText 7? Could you please advise?
UPDATE:
Such functionality has been indeed removed from iText7 and it was a conscious decision. iText7 provides all the means to put a barcode to a PDF document, however generating an image is a different story.

Related

How to write on images using EmguCV in C#

I wanna write on images using EmguCV in Windows Form C# In actual I wanna create a simple app for images as an image editor. Its simple purpose is to open images through an open file dialogue I can open the file but now I can't get any help or resource on How to write on images?. Let me explain my question through some image examples:
Image Before Editing:
Image after Editing:
So how can I do this using EmguCV in C#
I am newbie in dealing with images if there any way so kindly tell me.
And If there is not a such function involved in writing on images using EmguCV then please tell the alternative if any of you know??
Thanks in advance.
You can write on images with CvInvoke.PutText, one simple example is here:
// load the image
Mat image = new Mat("test.png");
// write "hello!" in red(0,0,255) at 100,100 coordinate from the top left corner
CvInvoke.PutText(image, "hello!", new System.Drawing.Point(100, 100), FontFace.HersheySimplex, 1.0, new MCvScalar(0, 0, 255), 2);
// display the result
CvInvoke.Imshow("Image with text", image);
// wait for user input to dismiss the image
CvInvoke.WaitKey();
// free allocated memory
image.Dispose();
Seems confusing for somebody new to EmguCV but it really is just a matter of looking at what each parameters do, more info in the EmguCV official documentation.

Ghostscript exports PNG but cuts half of the landscape page

I am trying to convert pdf to picture with Ghostscript. I have a landscape A3 PDF but no matter what I do, I only get the left half of every page.. I tried adding the -dPDFFitPage, -dFIXEDMEDIA, and other parameters but it didn’t seem to change anything..
My code without additional customswitches (C#):
var r=new GhotsScriptRasterizer();
r.open(pdfPath);
var pdf2png=r.GetPage(300,300,1);
pdf2png.Save(savePath);
I am using Ghostscript 8.64 32bit.
Thanks for the comments. It was really the version - GS 9.26 32Bit worked for me. Thanks!

How to draw vector graphics and save it in PDF file?

I'm working on a project which generally is collecting data and drawing results in charts. (Using C#) I need to save my charts in a PDF file. My question is, how to save charts in a PDF file without loosing resolution? My point is how to draw vector graphics instead of raster graphics?
I tried iTextSharp to create PDF file but the result is not satisfying at all!
I'm new here, so I'm not able to upload pictures.
Here is the result after saving my file:
https://www.dropbox.com/s/ruwtc82hfosxk6y/Test.pdf?dl=0
Here is the PDF that I need to create:
https://www.dropbox.com/s/jvu5uu069imo9xc/nir%20well%20abfar.pdf?dl=0
There are two ways I know of to get high-quality images from your Chart into a PDF.
One is by using vector formats:
Use chart.SaveImage with one of the three emf formats.
Convert the resulting emf file to wmf
Insert the wmf file into your iTextSharp document.
1 and 3 are one-liners. But step 2 is not. In fact I haven't found a working c# solution at all. The best was a weird reference to an edit that has disappeared here ..
If you can use some other program to do the conversion you will get nice results like this demo pdf file.. I used Illustrator for the conversion.
Two: If you can't get step 2 to work, you can still get nice results, if you use raster images with a nice and high resolution. Here is how to do it:
First we hide the Chart, so we don't scare the user. Then we make it as large as we want the output to be. Then we DrawToBitmap and finally we reset the chart again..:
Size s = chart1.Size;
chart1.Hide();
// pick your size in pixels
// I simply multiply my screen size..:
chart1.Size = new System.Drawing.Size(s.Width * 5, s.Height * 5);
using (Bitmap bmp = new Bitmap(chart1.ClientSize.Width, chart1.ClientSize.Height))
{
// you should set the resolution,
// although I didn't find a way for iTextSharp to use it visually
bmp.SetResolution(600, 600);
using (Graphics G = Graphics.FromImage(bmp))
{
G.SmoothingMode = SmoothingMode.HighQuality;
G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
chart1.DrawToBitmap(bmp, chart1.ClientRectangle);
bmp.Save(yourImageFile, ImageFormat.Png);
}
}
chart1.Size = s;
chart1.Show();
You could also use SaveImage and save a few lines, but you can't set the resolution of the png file there and it will be saved at the currnt screen resolution, which is 96dpi here..
Now you have a large image and will probably have to scale it down in iTextSharp:
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(yourImageFile);
img .ScalePercent(15); // scale to fit your needs..
doc.Add(img );
Note that the legend and the labels get very small this way, so you may have to enlarge them before saving. I also found that, when scaling down, the image is rather bright until you zoom in..
Here are two screenshots, one from the chart, the other from the pdf documnet after zooming in a lot (300%)..:

Windows API Code Pack - ShellFile not generating PDF bitmap

Using the code from previous stack overflow questions:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
I am trying to use window API to get the thumbnail of a PDF
I am led to believe that this generates an image file that resembles the first page of the PDF document.
The reality however is that it does NOT look like that, and merely looks like the PDF icon.
Is there anything that I'm missing that is required before this actually works as intended?
PDF files are correctly associated with adobe reader.
When browsing directories in windows explorer I DO see thumbnails associated with the documents.
I should note that the code DOES in fact correctly extract thumbnails when dealing with Excel and Word documents.
EDIT (references):
C# get thumbnail from file via windows api
Get thumbnail of any file, not only image files on Windows XP/Vista
Windows API Code Pack Thumbnail gives preview thumb of pdf but not Word or Excel
You need to specify that you want the thumbnail, not the icon (the default).
Change your code into this:
System.Drawing.Bitmap image;
ShellFile f = ShellFile.FromFilePath(fileLocation);
//force the actual thumbnail, not the icon
f.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly;
image = f.Thumbnail.ExtraLargeBitmap;
image.Save(tempfile, ImageFormat.Png);
The problem is because you have not selected the active frame that you will create the thumbnail from.
I cannot verify it on my current machine, because I don't have the Windows API on it, but it's giving you the standard PDF thumbnail because in your code you have't specified which page to use for the thumbnail.
Try doing something like this:
Image image = new Image();
//Load image here
int frameindex = 1; // Page number you want to use for thumbnail
Guid guide = image.FrameDimensionsList[0];
FrameDimension fDimension = new FrameDimension(guide);
image.SelectActiveFrame(fDimension, frameindex);
//Then go on to extract your thumbnail
I was not able to get ExtraLargeBitmap to work for PDF files but all other sizes (Large, Medium, and Small) worked fine.
Dim MyShellFile As ShellFile = ShellFile.FromFilePath(fi.FullName)
Dim MyThumbNail As Image
MyShellFile.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly
MyThumbNail = MyShellFile.Thumbnail.LargeBitmap
Me.PictureBox2.Image = MyThumbNail

XPS Document get trimmed on WPF DocumentViewer

I have an XPS document generated in code which is having a image visual. I am assigning the same XPS Document to a WPF documentViewer control as shown below in the code snippet:
.cs File
xp = new XpsDocument(pack, CompressionOption.Fast, URIAdress);
FixedDocumentSequence fixedDocumentSequence = xp.GetFixedDocumentSequence();
documentViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;
.XAML File
<DocumentViewer Name="uxDocumentViewerWithImage" Width="Auto" />
Problem: When I luanch my client to see the document viewer, it trimmed the image of XPS document from right hand side. One solution is to reduce the size of image, but due to requirements I can not modify the image size.
Please suggest how can I make the image visible fully on the document viewer?
Update:
I tried the following solution it worked but with assigning hardcode width value:
(fixedDocumentSequence as IDocumentPaginatorSource).DocumentPaginator.GetPage(pageIndex).Visual as FixedPage).Width = widthToAdjust; // Hard Code Width value
Still looking for more generic aproach here, like moving to LandScape mode ? but do not know how :(

Categories