I'm kind of confused as to the way C# saves bitmaps. I currently use this statement to save a bitmap to (virtually) any extension (.ico, .dds, etc.).
Bitmap.Save(FileName);
This actually works for any extension I choose, but I'm having doubts it's saving in correct format. Reason for said doubts is because I found another article here,
How to save Bitmap as icon?, which does so like:
// Create a Bitmap object from an image file.
Bitmap bmp = new Bitmap(sFn);
// Get an Hicon for myBitmap.
IntPtr Hicon = bmp.GetHicon();
// Create a new icon from the handle.
Icon newIcon = Icon.FromHandle(Hicon);
//Write Icon to File Stream
System.IO.FileStream fs = new System.IO.FileStream(destFileName, System.IO.FileMode.OpenOrCreate);
newIcon.Save(fs);
Which is obviously way different. So two questions:
How can/does the first approach work if the second is the "correct" way?
Would I need to customize save properties for each individual extension I wish to support or is the first method sufficient for any extension?
The proper way would be to use the original Bitmap.Save(Filename) but you are missing the ImageFormat variable which will basically do the actualy Conversion from an image obejct ot the specified format.
Currently the way you are using it results in a file named "somefile.somextension" but does not have the actual contents of an icon object for example.
best way would be to use :
Bitmap.Save(String, ImageFormat)
for example, to save a .ico file :
Bitmap.Save(String, System.Drawing.Imaging.ImageFormat.Icon)
Hope this helps, good luck.
Related
I have the requirement to create an Bitmap Object from a Excel Path inside a ms-word document, and I can't seem to find a way to do it in one step.
The current solution is the following.
Create a Field Object
Unlink the Field Object
Like this:
Dim fld As Field
Set fld = Selection.Fields.Add(Selection.Range, WdFieldType.wdFieldLink, "Excel.Sheet.12 ""[Path to Xlsx]"" ""[Sheet+Range]"" \b", False)
fld.Update
fld.Unlink
\b says the field should represented as a Bitmap object.
With fld.Unlink I am dropping the link and the Bitmap stays.
Is there a way to directly create the Bitmap Object (InlineShape) without the field approach?
I tried to record a macro in ms-word to see how ms-word is doing it but saddly it only gives:
Selection.PasteSpecial Link:=False, DataType:=wdPasteBitmap, Placement:= wdInLine, DisplayAsIcon:=False
If possible I would like to avoid working with Clipboard and PasteSpecial.
Edit:
Since the given answer has shown me that my question wasn't as clear as I hoped it to be.
So first of all, the only Information I currently have are
The path to an XLSX file
The Sheet and Range of said XLSX File
eg.
C:\test\myWorkbook.xlsx
Sheet1!A1:D10
Thats all the Information I have.
With this Information I need to programatically create a Bitmap inside my ms-word document.
As shown above I already have a solution which does exactly this, by doing a LINK to the said XLSX file and after it got updated dropping that link to only have the Bitmap left.
A Bitmap is staying here because of the \b flag which tells the LINK to produce a Bitmap, instead of a RTF/HTML.
My question now is, if there is another way I am currently doing it.
The current answer, does indeed insert a Bitmap into my ms-word document, but where do I get the Bitmap from, as the answer isn't aware of the fact that I need an image of an excel workbook? As I said earlier, I only have limited information and no Bitmap.
What I want to achieve can be done without programatically by using the Paste Special functionality
https://support.office.com/en-us/article/paste-special-e03db6c7-8295-4529-957d-16ac8a778719
Open Xlsx/Docx
Copy Cells from Xlsx
Go to Docx
Use Paste Special
Select Bitmap
Hit OK
The output you receive in your Docx is what I desire to make programatically, without Clipboard and Paste Special and with a, if possible, better solution than I currently have.
You can use the following code:
string fileName = "c://msdn.bmp"; //the picture file to be inserted
Object oMissed = doc.Paragraphs[2].Range; //the position you want to insert
Object oLinkToFile = false; //default
Object oSaveWithDocument = true;//default
doc.InlineShapes.AddPicture(fieldName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
See insert a picture into a word document using c#? for more information.
So, I am making a project in Windows Forms in which I have to change image formats. I found different solutions for that, such as:
convert tiff to jpg format
c# convert image formats to jpg.
And these solutions are working, but they all have one similarity:
image.Save(path,ImageFormat.Jpeg)
In this code I am saving the image in one directory. What I want to do is to save this formatted images with Savefiledialog. But in this case, when I call Savefiledialog, I need to write "image" name and format. If I don't write the format, it creates a simple file without extension. I want to automatically save the file in the specified format. So how to do that?
I would write something simple like this:
// might make sense to make this a property under the relevant class
Dictionary<string, ImageFormat> mapOfExtensions =
new Dictionary<string, ImageFormat>
{
{".jpg", ImageFormat.Jpeg},
{".jpeg", ImageFormat.Jpeg},
{".png", ImageFormat.Png}
};
var ext = Path.GetExtension(path);
// safety check - make sure the file extension is what we expect...
if (!mapOfExtensions.Contains(ext))
{
// probably would make sense to throw an error, log a message, etc.
return;
}
image.Save(path,mapOfExtensions[ext]);
I have created sort of a cross language Random Texture Generator using C# and Java to help design backgrounds for an iPhone app that I am designing. In short, my program first generates a random array of bytes to represent the texture in the Java portion, then the C# portion takes the bytes and creates a Bitmap which is saved to an image file (Jpeg,Png,etc). I have gotton my program to run perfectly except that when I save the Bitmap to a file it doesn't save with an extension, So I can't view the image after it is created. I have this line of code:
bitMap.Save("testImage", System.Drawing.Imaging.ImageFormat.Jpeg);
which should save the bitmap with a Jpeg extension, but it saves it with no extension. Could anyone help me with this conundrum?
If you're saving the bitmap with a jpeg extension (.jpg or .jpeg), you need to match the file format to the file extension.
It seems you want to support multiple extensions. To do this, I'd use the following:
string extension = "";
switch (format)
{
case (System.Drawing.Imaging.ImageFormat.Jpeg);
extension = ".jpg"; break;
.
.
.
}
bitMap.Save("testImage"+extension, System.Drawing.Imaging.ImageFormat.Jpeg);
Where you create an extension based on the desired file format, then use that extension to save the file.
The first parameter of Bitmap.Save method in MSDN defined as
filename
Type: System.String
A string that contains the name of the file to which to save this Image.
You must use this method as
bitMap.Save("testImage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
See documentation for more info.
What's the difference between File.WriteAllBytes and FileStream.Write/WriteBytes? I have a bitmap object that and I want to create a new bmp/jpg/png on disk. I think I read somewhere that WriteAllBytes uses FileStream.Write underneath?
WriteAllBytes is just a convinience method, that wraps the underlying Stream operations. (Create a file, write to stream, close stream, etc). Use it if it fits your needs. If you need more control on the underlying operations, fallback to using a Streamor similar.
It is all about using the right abstraction for the task.
Use WriteAllBytes to just save all the bytes, use Write if you need to watch the progress.
You're on the wrong track with this. Saving a bitmap object requires Image.Save(). That's a method that knows how to use an image encoder to convert a bitmap into the bytes that another program (or yours) can load back. There are several image encoders, you can select the one you want with the Save() overload that lets you pick the ImageFormat. The BMP format is the native Windows format, it is uncompressed. The PNG format is nice, it is a compressed lossless format. The JPEG format is a compressed lossy format, good for photos. File size is big to small in order.
You should use WriteAllBytes if you want to save a bitmap.
I hooked into the question located at "Replace image in word doc using OpenXML". I noticed that there are several ImagePartType types. Is there any easy "built in" way to determine which type a specific image should be other than going by it's extension? The ImagePartType enum is also used in PowerPoint as is alot of the WordProcessingML structures.,
For instance,
ImagePartType.Bmp on image1.bmp
ImagePartType.Emf on image1.emf
ImagePartType.Gif on image1.gif
ImagePartType.Icon on image1.ico
ImagePartType.Jpeg on image1.jpeg or image1.jpg
ImagePartType.Pcx on image1.pcx
ImagePartType.Png on image1.png
ImagePartType.Tiff on image1.tiff or image1.tif
ImagePartType.Wmf on image1.wmf
There is not - because you can have filename.png which is actually a bmp file and it all works fine. The only way to know for sure is to read the actual bitmap file and see what format it is in.
You can do this either by loading it into an Image class, or just read the first couple of bytes and look for the signature of each of the formats.