How do I produce VALID *.jpg in different sizes using .NET i.e. without setting invalid markers

  • Thread starter Thread starter Peter the Creative
  • Start date Start date
P

Peter the Creative

Guest
I am trying to make different versions of my *.jpg files using the code shown here:

protected static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
Bitmap result = new Bitmap(width, height);
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.DrawImage(image, 0, 0, result.Width, result.Height);
}
return result;
}
public static string opret_version(Billedbuf billeddata, string extension, int buforgWidth, int buforgHeight, int bufeditWidth)
{
Bitmap myNewBitmap = new Bitmap(HttpContext.Current.Server.MapPath(billeddata.DesignatedPath + Path.GetFileName(billeddata.Original_link)));
int bufeditHeight = calculate_Height(bufeditWidth, buforgWidth, buforgHeight);
string newfile = Path.GetFileNameWithoutExtension(billeddata.Original_link) + extension + ".jpg";
Bitmap myEditnail = ResizeImage(myNewBitmap, bufeditWidth, bufeditHeight);
myEditnail.Save(HttpContext.Current.Server.MapPath(billeddata.DesignatedPath + newfile),
System.Drawing.Imaging.ImageFormat.Jpeg);
myNewBitmap.Dispose();
return newfile;
}

I should say, that the extension in the line:

string newfile = Path.GetFileNameWithoutExtension(billeddata.Original_link) + extension + ".jpg";
has nothing to do with .jpg or the like. It is my way of naming the different copies of my image: If the original is joyfull.jpg then the extension_screen leads to a copy named joyfull_screen.jpg (to get a reasonable downloadtime when clicking the thumbnail, which is then joyfull_thumb.jpg). So there is no double extension.
And in a sense it works: In a web browser the images are correctly displayed. But when I try to use Adobe Photoshop to open files produced by this code I get:

df63dca715dc7eab431f64c53a5479e7._.png


Now this is not about photoshop. I need the images also for another application, a Windows Forms application that I am developing using Embarcaderos Delphi which I believe is more suitable for that. And Delphi also rejects these files. The original *.jpg (from my CANON camera) is opened without any problems, it is the resized (and renamed) variations that cause trouble.

I should say also, that the *.jpg images produced by the abovecited code can be opened by PAINT. And when I do that (following a hint in this forum) and then subsequently save them again from PAINT using the same filename then I get a message that all transparency will be lost (as if my files were transparent which they of course are not) and then the files thusly saved CAN be opened without error in Photoshop. But this is not a solution to my problem: I want C# .NET code to produce valid *.jpg files.

Now I have some 10.000 of these *.jpg files so of course this is a problem. I can see from my browsing around that many people seem to have problems with *.jpg files. How can I produce different sizes of an *.jpg file using code similar to the above listed (Visual Studio 2015) - i.e. *.jpg files that will be accepted as being indeed JPEG without any unknown or invalid marker types as illustrated by the inset. And without Delphi throwing an EInvalidGraphic exception?


Peter the Creative Timewaster

Continue reading...
 
Back
Top