resize an image stored in a folder - vba

I want to take an image from a folder resize it and store it in the same location with the same name. I saw a lot of examples of how to take an image, resize it and put it inside powerpoint slide but I just want to store it in a folder and not in the presentation. Is that possible?

Use Imagejpeg.
This works to resize a jpeg. It was written to take $content as a feed from a database - just replace with fread() of the iamge you wish to resize.
$newheight=floor($newheight);
$newwidth=floor($newwidth);
$image=imagecreatefromstring($content);
$newimage=imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newimage, $image, 0, 0, 0, 0, $newwidth, $newheight,$row['width'], $row['height']);
imagejpeg($newimage,$filetosaveto);

Related

Drawing a second text below the first text

I would like to draw 2 texts onto my PDF.
The first text should be aligned to the top left corner.
This works fine.
I'm using:
canvas = stamper.GetOverContent(i)
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
watermarkFontColor = iTextSharp.text.BaseColor.RED
canvas.MoveTo(0, 0) 'I think the canvas is the space that we draw onto. My documents always start at position X=0 and Y=0, so move to 0,0 should be fine
canvas.BeginText()
canvas.SetFontAndSize(watermarkFont, 12)
canvas.SetColorFill(watermarkFontColor)
canvas.ShowTextAligned(Element.ALIGN_TOP, uText, 0, 830, 0) 'is 830 the width of the available space?
canvas.EndText()
Now I would like to draw another text approximately 100 pixels below the first text.
I'm using:
canvas.MoveTo(0, 100) 'let's draw the second text at X=100, Y=100
canvas.BeginText()
canvas.SetFontAndSize(watermarkFont, 12)
canvas.SetColorFill(watermarkFontColor)
canvas.ShowTextAligned(Element.ALIGN_CENTER, uBewirtung, 0, 830, 0)
canvas.EndText()
The second text however doesn't show up at all.
I suspect I'm drawing outside the document, but I don't see my mistake.
The MoveTo() method is meant for drawing paths (lines amd shapes in graphics state), not text (in text state). It adds an m operator to the content stream. If you are a PDF specialist, you should use the SetTextMatrix() method inside your BT/ET text block: What does setTextMatrix of contentByte class in iText do?
Note the if; it is important. If you are not a PDF specialist, you shouldn't be toying around with those methods. You should use ColumnText.ShowTextAligned() instead of BeginText(), EndText() and all of the lines you added in-between. Those methods are meant for people who speak PDF syntax.

Actually cropping a PDF with PDF Clown

My objective is actually cropping a PDF file with PdfClown.
There are a lot of tools/library that allow cropping PDF, changing the PDF cropBox. This permits hiding contents outside a rectangular area, but content is still there, it might be accessed through a PDF parser and PDF size does not change.
On the contrary what I need is creating a new page containing only the contents inside the rectangular area.
So far I've tried scanning contents and selectively cloning them. But I didn't succeed yet. Any suggestions on using PdfClown for that?
I've seen someone is trying something similar with PdfBox Cropping a region from a PDF page with PDFBox not succeeding yet.
A bit late, but maybe it helps someone;
I am sucessfully doing what you are asking for - but with other libraries.
Required libraries : iText 4 or 5 and Ghostscript
Step 1 with pseudo code
Using iText, Create a PDFWRITER instance with a blank Doc. Open a PDFREADER object to the original file you want to crop. Import the Page, get a PDFTemplate Object from the source, set its .boundingBox property to the desired cropbox, wrap the template into an iText Image object and paste it onto the new page at an absolute position.
Dim reader As New PdfReader(sourcefile)
Dim doc As New Document()
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New System.IO.FileStream(outputfilename, System.IO.FileMode.Create))
//get the source page as an Imported Page
Dim page As PdfImportedPage = writer.GetImportedPage(reader, indexOfPageToGet) page
//create PDFTemplate Object at original size from source - see iText in Action book Page 91 for full details
Dim pdftemp As PdfTemplate = page.CreateTemplate(page.Width, page.Height)
//paste the original page onto the template object, see iText documentation what those parameters do (scaling, mirroring)
pdftemp.AddTemplate(page, 1, 0, 0, 1, 0, 0)
//now the critical part - set .boundingBox property on the template. This makes all objects outside the rectangle invisible
pdftemp.boundingBox = {iText Rectangle Structure with new Cropbox}
//template not needed anymore
writer.ReleaseTemplate(pdftemp)
//create an iText IMAGE object as wrapper to the template - with this img object absolute positionion on the final page is much easier
dim img as iTextSharp.Text.Image = Image.GetInstance(pdftemp)
// set img position
img.SetAbsolutePosition(x, y)
//set optional Rotation if needed
img.RotationDegrees = 0
//finally, this adds the actual content to the new document
doc.Add(img)
//cleanup
doc.Close()
reader.Close()
writer.Close()
The output file will visually look cropped. But the objects are still present in the PDF Stream. Filesize will probably remain very little changed yet.
Step 2:
Using Ghostscript and output device pdfwrite, combined with the correct command line parameters you can re-process the PDF from Step 1. This will give you a much smaller PDF. See Ghostscript documentation for the arguments https://www.ghostscript.com/doc/9.52/Use.htm
This steps actually gets rid of objects that are outside the bounding box - the requirement you asked for in your OP, at least for files that I deal with.
Optional Step 3:
Using MUTOOL with the -g option you can clean up unused XREF objects. Your original PDF probably had a lot of Xrefs, which increase filesize. After cropping some of them may not be needed anymore.
https://mupdf.com/docs/manual-mutool-clean.html
PDF Format is a tricky thing, normally I would agree with #Tilman Hausherr, my suggestion may not work for all files and covers the 'almost impossible' scenario, but it works for all cases that I deal with.

How to add dynamically created image to powerpoint slide?

I am creating a PPT file with VB.NET and I would like to insert dynamically created images from memory into the slides, these images are screenshots being put into Image variables.
oSlide.Shapes.AddPicture("file location", False, True, 150, 150, 500, 350)
Is used to insert an image but it only accepts a file location.
I would rather not save all of the images I am creating to insert then delete off my drive. Does anyone have a solution?
Clipboard.SetImage(YouVariableHere) Load your image to the clipboard first
This function happens to take a System.Drawing.Image as it's argument
Shapes.Paste pastes whatever is currently on the clipboard.

Original Image Size

Using interop.powerpoint in VB.Net I am inserting an EMF file with the code:
' add picture shape to slide
AvailableSlide.Shapes.AddPicture(FileName:=file, LinkToFile:=MsoTriState.msoFalse, _
SaveWithDocument:=MsoTriState.msoTrue, Left:=SlideHorizontalPosition, Top:=SlideVerticalPosition)
' save original image size
mOriginalImageWidth = CurrentShape.Width
mOriginalImageHeight = CurrentShape.Height
' if EMF then crop the bottom
CurrentShape.PictureFormat.CropBottom = mCropValue
I have the original size saved before cropping. However, if I'm using a selected image in a PPT file I didn't save, I can't figure out where the original image size is stored in the selected "shape" object (5.49" by 4.13" in the image below). I assumed it would be in the PictureFormat somewhere.
dgp
Set the shape's .ScaleHeight and .ScaleWidth to 1 to return it to the original size.
This may or may not work but it's worth a shot (I can't test because I don't have Powerpoint). After a little bit of research on the PictureFormat interface, it has a few members that might be useful to you. I didn't exactly see anything that specified the picture's original size, but there were these four properties that seemed helpful: CropLeft, CropRight, CropTop, and CropBottom. They return the number of points cropped off each side respectively. A way to get the original size of the object would be to add the Width to CropLeft + CropRight and add the Height to CropTop + CropBottom. Try that and let me know if it works. Documentation is found here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.pictureformat_members(v=office.14).aspx
Try getting the shape's ScaleWidth and ScaleHeight properties. I couldn't find it in the documentation, but awhile ago, I had a student work with me to create an application to format powerpoint presentations that were imported into microsoft word. Here's the code that dealt with the scale size of the shape.

VB.NET: Preserve image metadata when moved to clipboard as an image

Visual Studio 2010, .NET 4, VB.NET
Hello,
I am writing a little program to convert LaTeX snippets to images which can be pasted into whatever program one can paste images into. It's working alright but the next obvious step is to include the source LaTeX code as a piece of metadata in the image so that the results can be modified without having to retype everything.
I have succeeded in adding a title PropertyItem with the latex encoded as an ASCII byte array as its value (id=800, type=2, value=System.Text.Encoding.ASCII.GetBytes(codestring)). I verify that the PropertyItem is really there before trying to put the image on the clipboard.
Then I do Clipboard.SetImage(myImage). The result is all of the PropertyItems are removed (my title plus anything else that was there)! I check this by doing MsgBox(Clipboard.GetImage.PropertyItems.Count.ToString) which gives zero.
This makes me very sad. Anyone know what's up?
Thanks in advance!
Brian
Update: I have figured out how to move the image onto the clipboard and then back off while preserving the PropertyItems like so:
Format = DataFormats.GetFormat(GetType(Image).FullName)
Dim dataObject As New DataObject
dataObject.SetData(Format.Name, image)
Clipboard.SetDataObject(dataObject)
Dim copiedImage As Image = CType(Clipboard.GetDataObject.GetData(Format.Name), Image)
This way, the copiedImage has the same PropertyItems as the original. However, new problem:
Other programs don't recognize what's on the clipboard as an image anymore, which defeats the whole purpose. I.e., if I put an image on the clipboard this way, when I try pasting into some context that accepts pasted images, nothing happens.
What to do?!
I believe the Windows clipboard image has no metadata. If you change the format of the image to add metadata, it is no longer a clipboard image. If the other programs can accept it, you could copy and paste the image file (instead of the image) to the clipboard, and the metadata will of course be intact when it's read by the target app.
Have you tried Clipboard.SetData or Clipboard.SetDataObject? SetImage only copies the image in bitmap format, so I am not surprised that it strips the property items. You might try:
Clipboard.SetData(DataFormats.EnhancedMetafile, myImage)
or
Clipboard.SetData(DataFormats.MetafilePict, myImage)