What I see from a quick glance:
Streams should be wrapped using a template (...), in your case, if an exception occurs during processing, Dispose () will not be called.
using (FileStream fs = new FileStream(FilePath, FileMode.Open)) {
You should catch only those exceptions that you expect. For example, in SerializeImage it will be an IOException. Catching all exceptions is a very bad practice.
} catch (IOException ex) {
The Image.FromStream method depends on the stream, so if you close the base stream and return the image, you can get unpredictable behavior (well, in most cases this will work, but sometimes an error occurs). Therefore, you need to create a copy of the image and return it.
using (MemoryStream ms = new MemoryStream(ImageByteArray)) { using (Image img = Image.FromStream(ms)) { return new Bitmap(img); } }
You do not have the tg graphic and the img object in the SaveImage method (but the remote ImageObject, see the next paragraph). In general, I donβt see the need for such logic, just call ImageObject.Save (..., ImageFormat.Png) if you want to preserve the quality of saving images.
In the same method (SaveImage), you are in the ImageObject parameter. In most cases, this is also bad practice; consider disposing of this image outside the working method using a template (...).
source share