Hello everyone, i'm fresh new here, and i already have a question regarding...well, C#.
I've made a Forms Application in which the user submits an image (bmp,png,jpg or gif), and the image suffers some byte-level modifications (obviously, i'm using the LockBits() method).
Now, after the modifications are done, the user should be able to save the image in its original format (only the least important bits of the image get modified, so the image looks pretty much like the original).
I've developed a static class called ImageTextEncoder which has a public static member ImageTextEncoder.Image, which holds the submitted bitmap.
When i call ImageTextEncoder.Image.Save(Filename,Format) (if initially the image was a Jpeg, i will save it as a jpeg, same for png, gif, etc. ), and i upload the image back, visibly it suffered nothing, but its size gets modified (example: a 700 kb JPG got down to 140 kb), AND at byte level things are altered.
IS there a way i can save the image WITHOUT losing ANYTHING ( size, byte information) . (as a note, i need a solution asap, it's been bugging my application for some days now)
Thank you very much !
Well the only reason it worked was because it wasn't really a JPG at all. See if you can disable the compression for JPEG so it doesn't modify the file's contents. Beyond that i'm not sure off hand, this is getting very file-format specific.
your extention was jpg but the image was NOT a jpeg. extensions mean nothing. Listen. 1. If you want to preserve your changes you cannot compress it, because compression codecs change those values. 2. Saving as a BMP is uncompressed, So it will work, but the file will be big. 3. if the file is big, then its uncompressed, and your data is preserved, if the file is small, then its compressed and you WILL…
you could in theory take your image and load it in as a byte array, not as an image, sift through the bytes and find the start of the image data, then apply them there, but idk what the results would be. but other than that. there isn't sorry.
if the image is a different size, its probably the compression level. when you save your bitmap, if you save it as a jpeg file, you can alter its compression methods, changing its filesize, if you just call save, it uses the default. which might be different from the source image.
I ment size as in file size not dimensions. sorry.
// We will store the correct image codec in this object
ImageCodecInfo iciJpegCodec = null;
// This will specify the image quality to the encoder
EncoderParameter epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (int)80);//set quality here
// Get all image codecs that are available
ImageCodecInfo iciCodecs = ImageCodecInfo.GetImageEncoders();
// Store the quality parameter in the list of encoder parameters
EncoderParameters epParameters = new EncoderParameters(1);
epParameters.Param = epQuality;
// Loop through all the image codecs
for (int i = 0; i < iciCodecs.Length; i++)
// Until the one that we are interested in is found, which is image/jpeg
if (iciCodecs[i].MimeType == "image/jpeg")
iciJpegCodec = iciCodecs[i];
yourImage.Save(filepath, iciJpegCodec, epParameters);
I borrowed this code from somewhere a long time ago, i don't remember where. there are other ways. but this one always worked for me.
Ok, never mind GIF,PNG,BMP, i'm using the suggestion which you, and also microsoft http://support.microsoft.com/kb/324790 apparently gives, but my problem ain't solved - when i call the new parametrized save method, with 100 quality quoefficient, my image still gets shrinked :(.
The problem with image shrinkage is data loss - i used the least significative bits of each byte to encode some data, and that data is LOST when i save the image :(
please help !
the idea behind JPEG image compression is that it removes color differences that are not apparent by the human eye. If you really want a full quality picture you might want to always save to bmp or png24 regardless of source type.
jpeg compression looks good, and makes for a good file size, but if your goal is just to preserve bytes written into your image, you probably will loose it during the compression. Sorry. its still not clear to me what your goal is.
im not sure if this will suit u , especially i didn't understand completely
what u intend to do ..
but : what about submitting array of bytes representing the image..
and then reading them through memory stream then converting them to an image with the type u want
this is a part of code i used before for saving submitted images..
//this is a kind of encoding i used
byte img = Convert.FromBase64String(s);
MemoryStream ms = new MemoryStream(img);
Bitmap bmp = new Bitmap(ms);
Already tried that, the Save method will cause my image to lose bytes.
I am encoding text into an image - changing the least significant bit of each of the image's bytes to "encode" the ASCII representation of a character. (example: The letter "A" is 65 in ASCII -> 01000001 in binary, so i will modify 8 of the image's bytes in order to "encode" that specific letter).
I need to save any jpeg,bmp,gif,png file and not lose ANY of the image's bytes.
(I already tried saving Jpeg as a Bitmap, while still retaining the .jpg extension , but it increases the image's byte size too much. - it saved a 700 KB jpeg to a 3 MB one)
What i need is to save the image and not loose ANY of its bytes, while also retaining its original size in bytes ( 700 KB to 3 MB = NOT GOOD)
You ran the bytes through your converter.... In my last post I said to save the input stream directly to disk. Its hard to say whats going on here since we don't have the full code for your encoding/converting mechanisms. Are you writing the base-encoded bytes to disk and not the raw image to disk, and using your app to view the image?
>>So after using that method i flush down its bytes through the converter and save them to a file.
So what you are saying is "after I take an original image, run the image through a converter, and save the modified image it is different than my original image". Right? If you do anything to an image other than save it you're probably going to cause some change in it.
Wherever you are getting the source image -- before you convert it, encode it, or even load it in to an Image or Bitmap class in C# you should save the raw image bytes.
I dont need the original image saved back to the hard disk, i need the modified image saved to the disk. I modify only the least significant bits of the image, so the change isn't that big to increase its size threefold.
That's where my problem is.
I used your application and made that ~700kb jpeg to a ~2.xMB jpeg just like you said. However after inspecting the 2MB file I noticed it is actually a PNG file with a JPEG extension. You can change the file extension to another image type and it will still open in photo viewers properly. I'm sure you know that PNG is a lossless format so that makes sense why the file is so big.
Something you are doing is changing the image format.
I noticed here:
//Works only for JPG files, though it increases their size considerably.
//Shrinks bmp's, but works.