reduce image size
-
I am using this code after your modification to reduce the image size and save the reduced image into the xml file but it's still saving the large image not the reduced image. I tried using the reduced_image but it's not working..
picturePhoto.Image = System.Drawing.Image.FromFile(dialogPhoto.FileName);
Graphics reduced_images = Graphics.FromImage(Image.FromFile(dialogPhoto.FileName));
reduced_images.DrawImage(picturePhoto.Image, 0, 0, 200, 300);memory_stream = new MemoryStream();
picturePhoto.Image.Save(memory_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = memory_stream.ToArray();xml_document.GetElementsByTagName("Photo")[0].InnerText = Convert.ToBase64String(imageBytes);
xml_document.Save(public_var.file_name); -
I am using this code after your modification to reduce the image size and save the reduced image into the xml file but it's still saving the large image not the reduced image. I tried using the reduced_image but it's not working..
picturePhoto.Image = System.Drawing.Image.FromFile(dialogPhoto.FileName);
Graphics reduced_images = Graphics.FromImage(Image.FromFile(dialogPhoto.FileName));
reduced_images.DrawImage(picturePhoto.Image, 0, 0, 200, 300);memory_stream = new MemoryStream();
picturePhoto.Image.Save(memory_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = memory_stream.ToArray();xml_document.GetElementsByTagName("Photo")[0].InnerText = Convert.ToBase64String(imageBytes);
xml_document.Save(public_var.file_name);To resize an image,
var originalImage = Bitmap.FromFile(imagePath);
var resizedImage = new Bitmap(originalImage, newWidth, newHeight);I don't know about your requirements but I suggest you look into using a database but if you need to use an xml file may I suggest you save the images to disk and store the paths (relative paths) in the xml file since you'd be storing about 59 KB inside the xml file for each image which may increase the time required to parse the xml file and may make the xml file hit the size limit of the file system if you're storing a lot of images. Again, it's all about the requirements and what would be needed to be implemented in the future of the application.
Eslam Afifi
-
I am using this code after your modification to reduce the image size and save the reduced image into the xml file but it's still saving the large image not the reduced image. I tried using the reduced_image but it's not working..
picturePhoto.Image = System.Drawing.Image.FromFile(dialogPhoto.FileName);
Graphics reduced_images = Graphics.FromImage(Image.FromFile(dialogPhoto.FileName));
reduced_images.DrawImage(picturePhoto.Image, 0, 0, 200, 300);memory_stream = new MemoryStream();
picturePhoto.Image.Save(memory_stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = memory_stream.ToArray();xml_document.GetElementsByTagName("Photo")[0].InnerText = Convert.ToBase64String(imageBytes);
xml_document.Save(public_var.file_name);jrahma wrote:
picturePhoto.Image = System.Drawing.Image.FromFile(dialogPhoto.FileName); Graphics reduced_images = Graphics.FromImage(Image.FromFile(dialogPhoto.FileName));
that is creating two images, i.e. two distinct instances of the Image class, each holding the same picture. No wonder you will get confused. Furthermore, you should keep the image in a variable, as images need to be disposed of when done. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
-
To resize an image,
var originalImage = Bitmap.FromFile(imagePath);
var resizedImage = new Bitmap(originalImage, newWidth, newHeight);I don't know about your requirements but I suggest you look into using a database but if you need to use an xml file may I suggest you save the images to disk and store the paths (relative paths) in the xml file since you'd be storing about 59 KB inside the xml file for each image which may increase the time required to parse the xml file and may make the xml file hit the size limit of the file system if you're storing a lot of images. Again, it's all about the requirements and what would be needed to be implemented in the future of the application.
Eslam Afifi
actually i need to be in XML not database
-
jrahma wrote:
picturePhoto.Image = System.Drawing.Image.FromFile(dialogPhoto.FileName); Graphics reduced_images = Graphics.FromImage(Image.FromFile(dialogPhoto.FileName));
that is creating two images, i.e. two distinct instances of the Image class, each holding the same picture. No wonder you will get confused. Furthermore, you should keep the image in a variable, as images need to be disposed of when done. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
Luc Pattyn wrote:
Furthermore, you should keep the image in a variable, as images need to be disposed of when done
I'm fiddling with a gallery creation web page, I have the images in a folder but do not have any thumbnails. I wrote a loop through a datatable of image names and check for a thumb, creating a new thumb if it is missing. I get an outofmemory exception on the second thumb creation, I presume this is potentially caused by not disposing of the image!
Never underestimate the power of human stupidity RAH
-
Luc Pattyn wrote:
Furthermore, you should keep the image in a variable, as images need to be disposed of when done
I'm fiddling with a gallery creation web page, I have the images in a folder but do not have any thumbnails. I wrote a loop through a datatable of image names and check for a thumb, creating a new thumb if it is missing. I get an outofmemory exception on the second thumb creation, I presume this is potentially caused by not disposing of the image!
Never underestimate the power of human stupidity RAH
Yes, rather than
Graphics reduced_images = Graphics.FromImage(Image.FromFile(dialogPhoto.FileName));
...
reduced_images.Dispose(); // not present in OPit should be
Image img=Image.FromFile(dialogPhoto.FileName);
Graphics reduced_images = Graphics.FromImage(img);
...
reduced_images.Dispose();
img.Dispose();so both the Graphics and the Image instance get disposed of, keeping you out of OOM trouble. BTW: using a
using
construct would make it a bit more elegant. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
-
Yes, rather than
Graphics reduced_images = Graphics.FromImage(Image.FromFile(dialogPhoto.FileName));
...
reduced_images.Dispose(); // not present in OPit should be
Image img=Image.FromFile(dialogPhoto.FileName);
Graphics reduced_images = Graphics.FromImage(img);
...
reduced_images.Dispose();
img.Dispose();so both the Graphics and the Image instance get disposed of, keeping you out of OOM trouble. BTW: using a
using
construct would make it a bit more elegant. :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
Thanks Luc
Luc Pattyn wrote:
using a using construct would make it a bit more elegant.
I rarely need an object for a really short period and forever forget the use using, this obviously lends itself to that model, thanks for the reminder!
Never underestimate the power of human stupidity RAH
-
Thanks Luc
Luc Pattyn wrote:
using a using construct would make it a bit more elegant.
I rarely need an object for a really short period and forever forget the use using, this obviously lends itself to that model, thanks for the reminder!
Never underestimate the power of human stupidity RAH
You're welcome. PS: even when
using
doesn't fit the application, don't forget to dispose of long lived DataTables! :)Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.