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.
Related
I'm trying rendering text get from pdf file using iText7.
I use OpenTK to render graphics, and use SharpFont to convert font file to bitmap data.
The constructor of SharpFont.Face used to convert to bitmap, needs font data for arguments as file full path or raw byte array loaded from file.
The PdfFont.fontProgram instance seems to has embedded stream named "fontFile" (using debugging IDE), but I can not get the data.
Do you know how to get the data ?
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'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.
Im creating a new Bitmap object as below,
var image = new Bitmap(#"C:\file.jpg");
I will be doing several modifications in pixel levels in the spatial domain and if save this object again as below.
image.Save(#"D:\final.jpg", ImageFormat.bitmap);
Is this final image is a really a jpg or a bitmap ? ,
Using this functional is there a way we can save a jpg lossless ?
image.Save(#"D:\final.jpg", ImageFormat.Bmp);
will save a bitmap image in spite of the extension
Jpg is a lossy compression method, if you want loseless you can use either Bmp or Png
The Bitmap class is always in a 'raw' format. So what happens in your code is:
Read from a JPEG file (lossy format) into a Bitmap class (raw lossless format).
Save the contents of the Bitmap class into another file (BMP lossless format).
The contents of the Bitmap class after that is still in raw lossless format so that you can continue to manipulate it, or save it to file in yet another format (lossless or lossy).
Note: The 'raw' format I mentioned above is not the same format as some .raw files that you sometimes encounter in music editing programs. By 'raw' format I just mean some temporary arbitrary format that Bitmap uses to store the image data in itself (possibly in byte[]).
Is C# bitmap supporting saving the object to JPEG or PNG file format?
Bitmap extends Image, therefore you can call: Image.Save (String, ImageFormat). For example:
using System.Drawing
// ...
Bitmap img = new Bitmap("file.jpg");
img.Save("file.png", ImageFormat.Png); // ImageFormat.Jpeg, etc
Omitting the second argument and just calling Image.Save(String) will save the image as its raw format.
In addition to last post (can't comment existing post since i'm new here)
File type is NOT based off of the extension.
Just try to do
img.Save("result.bmp")
and
Image.Save("result.bmp", ImageFormat.Bmp);
and you'll see that file sizes are dramatically different.