Problem saving a resized image - HELP!!
-
I now have a problem with a piece of code which worked before! My code to get an image object from a directory is: System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imgFullPath); Then I resize it as follows: System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); In the debugger, fullSizeImg and thumbNailImg are {System.Drawing.Bitmap} so they must be ok. However, when I use the following code to save the image: thumbNailImg.Save(fullPath); I get the error message: CS0103: The name 'thumbNailImg' does not exist in the current context However, if I try and save the fullSizeImg (before resizing) this works fine, so I'm totally confused and desperate for help!:confused:
Forgive me if i'm incorrect in saying this but don't you need to create a NEW instance of the thumbNailImg first? :cool:
"Sex is not the answer, it's the question and the answer is yes"
-
Forgive me if i'm incorrect in saying this but don't you need to create a NEW instance of the thumbNailImg first? :cool:
"Sex is not the answer, it's the question and the answer is yes"
I'm a newbie to all this (so please bear with me :-)) - I found the vb version of this code on the web and attempted to rewrite as C#. I thought I was creating a new instance in this line: System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); If not, please tell me how. Many thanks Lorna
-
I'm a newbie to all this (so please bear with me :-)) - I found the vb version of this code on the web and attempted to rewrite as C#. I thought I was creating a new instance in this line: System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); If not, please tell me how. Many thanks Lorna
are they in the same method ?
-
are they in the same method ?
Yes and I'm sure it was working yesterday although I have extended my code a bit :confused:
-
Forgive me if i'm incorrect in saying this but don't you need to create a NEW instance of the thumbNailImg first? :cool:
"Sex is not the answer, it's the question and the answer is yes"
I dont think so, Because GetThumbnailImage returns an Image object and thumbNailImg is also an Image object. Am I right?
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
-
Yes and I'm sure it was working yesterday although I have extended my code a bit :confused:
can you please give the exact code(do a copy paste)?
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
-
can you please give the exact code(do a copy paste)?
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
I am using populating a ListBox with the files I want to resize and when the user clicks a button, the method goProcessImages is called and I traverse through each one resizing it (well I'm trying to!). Thanks - here is my code behind (sorry it's a bit of a mess but I've been commenting out things to find out what's going wrong): using System; using System.Collections; using System.Configuration; using System.Drawing; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; public partial class ResizeImage : System.Web.UI.Page { ArrayList imageNames = new ArrayList(); string imagesDir = "D:\\Website\\BookImages\\"; //get path of Books dir in website protected void Page_Load(object sender, EventArgs e) { string imgForLB = ""; if (!IsPostBack) { int i = 0; try { foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img.Remove(0, 22)); } foreach (string img in Directory.GetFiles(imagesDir, "*.jpeg")) { imageNames.Add(img.Remove(0, 22)); } foreach (string img in Directory.GetFiles(imagesDir, "*.gif")) { imageNames.Add(img.Remove(0, 22)); } } catch { Response.Write("We are currently experiencing problems in obtaining images from the directory " + imagesDir + "Make sure you are offline and this directory exists on your laptop and it is not empty."); } //write out image names to screen for Angell to check for (i = 0; i < imageNames.Count; i++) lbFileNames.Items.Add(imageNames[i].ToString()); } } // Required by GetThumbnailImage() method, but not used public bool ThumbnailCallback() { return true; } protected void goProcessImages(object sender, EventArgs e) { string imageName = ""; //resize each file listed in listbox foreach (ListItem item in lbFileNames.Items) { imageName = item.ToString(); if (imageName != "") { //createThumbnail(imageName, 400); createThumbnail(imageName, 100); } } } protected void creat
-
I now have a problem with a piece of code which worked before! My code to get an image object from a directory is: System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(imgFullPath); Then I resize it as follows: System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); In the debugger, fullSizeImg and thumbNailImg are {System.Drawing.Bitmap} so they must be ok. However, when I use the following code to save the image: thumbNailImg.Save(fullPath); I get the error message: CS0103: The name 'thumbNailImg' does not exist in the current context However, if I try and save the fullSizeImg (before resizing) this works fine, so I'm totally confused and desperate for help!:confused:
Member 3402886 wrote:
CS0103: The name 'thumbNailImg' does not exist in the current context
Show your full code.
Member 3402886 wrote:
fullSizeImg.GetThumbnailImage
Resizing image using
GetThumbnailImage
is inefficient and it returns only the thumbnail image attached to each image. So you will get pretty poorly resized image. Create aBitmap
with the new size and width, load your image into aGraphics
instance, useDrawImage()
to draw image into the required size. Save the bitmap. You will get image resized without loosing quality.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
I am using populating a ListBox with the files I want to resize and when the user clicks a button, the method goProcessImages is called and I traverse through each one resizing it (well I'm trying to!). Thanks - here is my code behind (sorry it's a bit of a mess but I've been commenting out things to find out what's going wrong): using System; using System.Collections; using System.Configuration; using System.Drawing; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.IO; public partial class ResizeImage : System.Web.UI.Page { ArrayList imageNames = new ArrayList(); string imagesDir = "D:\\Website\\BookImages\\"; //get path of Books dir in website protected void Page_Load(object sender, EventArgs e) { string imgForLB = ""; if (!IsPostBack) { int i = 0; try { foreach (string img in Directory.GetFiles(imagesDir, "*.jpg")) { imageNames.Add(img.Remove(0, 22)); } foreach (string img in Directory.GetFiles(imagesDir, "*.jpeg")) { imageNames.Add(img.Remove(0, 22)); } foreach (string img in Directory.GetFiles(imagesDir, "*.gif")) { imageNames.Add(img.Remove(0, 22)); } } catch { Response.Write("We are currently experiencing problems in obtaining images from the directory " + imagesDir + "Make sure you are offline and this directory exists on your laptop and it is not empty."); } //write out image names to screen for Angell to check for (i = 0; i < imageNames.Count; i++) lbFileNames.Items.Add(imageNames[i].ToString()); } } // Required by GetThumbnailImage() method, but not used public bool ThumbnailCallback() { return true; } protected void goProcessImages(object sender, EventArgs e) { string imageName = ""; //resize each file listed in listbox foreach (ListItem item in lbFileNames.Items) { imageName = item.ToString(); if (imageName != "") { //createThumbnail(imageName, 400); createThumbnail(imageName, 100); } } } protected void creat
Member 3402886 wrote:
try { // create the actual thumbnail image System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); }
Because of you are creating the variable inside try block, it is showing the error. should be like this System.Drawing.Image thumbNailImg; try { thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); }
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
-
Member 3402886 wrote:
try { // create the actual thumbnail image System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); }
Because of you are creating the variable inside try block, it is showing the error. should be like this System.Drawing.Image thumbNailImg; try { thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); }
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
Thanks - tried that but get message: CS0165: Use of unassigned local variable 'thumbNailImg' on line: thumbNailImg.Save(fullPath); Thanks Lorna