dcsimg
Results 1 to 13 of 13
  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,685

    Image file is blank?

    This post is related to a previous post: http://www.vbforums.com/showthread.p...&is_resolved=1

    I have attached the C# project. The problem I'm having is that when the new Image files are created, they are blank or black. There's nothing in them even though they were recreated with a new size. I don't have that much experience with the Image/Graphics objects to know what I'm doing wrong. The program isn't finished yet.

    Thanks,
    Attached Files Attached Files
    Blake

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,644

    Re: Image file is blank?

    Rather than attaching a project as a first option, could you post the relevant code directly? Also, unless you have some image resources in there, the size of your attachment suggests that you may not have deleted the bin and obj folders before zipping, which you need to do in order to follow the "no binary attachments" rule.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,403

    Re: Image file is blank?

    I don't have the right version of C# so I just looked at the code a bit in Notepad.
    The main issue is that you create a new bigger image bitmap, but then use that new image as the source of your drawImage method so are drawing the empty bitmap to itself.
    Code:
                    using (Graphics GFX = Graphics.FromImage((Bitmap)newImage))
                    {
                     //GFX is drawing newImage to newImage
                        GFX.DrawImage(newImage, new Rectangle(Point.Empty, sz)); 
                    }
    What you want to draw is the image that you loaded earlier, but you have already disposed of it before you get to this block.
    You can probably nest the code in the Using block you have, although I've done almost no C# so should test it myself.
    Or perhaps, don't use the Using block, and just dispose of loaded Image after you've used it.
    Code:
                    using (var image = Image.FromFile(filePath))
                    {
                        imageWidth = (int)(image.Width * pct);
                        imageHeight = (int)(image.Height * pct);
    
                        Size sz = new Size(imageWidth, imageHeight);
                        Image newImage = new Bitmap(sz.Width, sz.Height);
    
                        using (Graphics GFX = Graphics.FromImage((Bitmap)newImage))
                        {
                            GFX.DrawImage(image, new Rectangle(Point.Empty, sz));
                        }
                    }
    //----- Or ------
                    var image = Image.FromFile(filePath))
    
                    imageWidth = (int)(image.Width * pct);
                    imageHeight = (int)(image.Height * pct);
    
                    Size sz = new Size(imageWidth, imageHeight);
                    Image newImage = new Bitmap(sz.Width, sz.Height);
    
                    using (Graphics GFX = Graphics.FromImage((Bitmap)newImage))
                    {
                        GFX.DrawImage(image, new Rectangle(Point.Empty, sz));
                    }
                    image.Dispose();
    In either case, you load image from the file, create a larger newImage, and draw image to newImage.

    That is it in principle.
    As mentioned, I've only written a couple of things in C# so I might have syntax issues and haven't tried the code here, but at least you should see what the issue is with your existing code.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,685

    Re: Image file is blank?

    Passel,

    Your code worked. I did get the right image object to save correctly. However, the new image sizes are barely reduced. For example, I want to reduce the size of the image by 70%. After I did the resizing and saved the image, the size of the image was only reduced my by 5KB. The original image size was 3720KB and the new size is 3715KB. I checked the variables imageHeight and imageWidth after they acquired their new values. That calculation worked but saving the info didn't seem to do the trick. Below is the routine that performs all this.

    Code:
            private void btnResizeImages_Click(object sender, EventArgs e)
            {
                foreach (ListViewItem item in lv1.Items)
                {
                    var filePath = item.Text;
                    var newPath = txtNewImagePath.Text;
                    var filename = Path.GetFileNameWithoutExtension(item.ToString());
                    int imageWidth;
                    int imageHeight;
                    double pct = double.Parse(comboBox1.SelectedItem.ToString()) / 100;
    
                    DirectoryInfo newDir = Directory.CreateDirectory(txtNewImagePath.Text);
    
                    using (var image = Image.FromFile(filePath))
                    {
                        imageWidth = (int)(image.Width - ((int)image.Width * pct));
                        imageHeight = (int)(image.Height - ((int)image.Height * pct));
    
                        Size sz = new Size(imageWidth, imageHeight);
                        Image newImage = new Bitmap(sz.Width, sz.Height);
    
                        using (Graphics GFX = Graphics.FromImage((Bitmap)newImage))
                        {
                            GFX.DrawImage(image, new Rectangle(Point.Empty, sz));
                        }
    
                        newPath = newPath + @"\" + filename + "rz.jpg";
                        image.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    
                        lv2.Items.Add(newPath);
                        image.Dispose();
                    }
                }
            }
    Blake

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,403

    Re: Image file is blank?

    The files are compressed (i.e. jpg and png files are much smaller than the image's raw pixel format is), which means that most of the redundant information is already squeezed out of them. So, the size of the file may already be only 10% of the size of the full image as displayed (i.e. 90% of the raw redundant image data was squeezed out). Now you reduced the 100% expanded raw data to perhaps 30% of the raw data by resizing the image to a smaller image, which is essentially removing 70% of the raw data, probably much of it in the "redundant" category. Now when that 30% gets compressed by the jpg compression, you'll end up with pretty much the same size as before because you're compressing the 30% back down to around 10%, perhaps 9.5%.

    You can't save a lot of data by trying to compress a compressed file. In some cases, compressing an already compressed file just ends up creating a slightly larger file.

    Since your smaller image still looks pretty much like your original larger image (you hopefully can still make out what it is you're looking at), those unique pixels that make it recognizable won't compress that much. You can only compress by removing redundant information or coding the unique data in a shorter number of bits than the unique data took originally.
    Last edited by passel; May 7th, 2018 at 01:08 PM.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,685

    Re: Image file is blank?

    Ok, so how then can I manipulate the size and dimensions of an image like the "Paint" program?
    Blake

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,403

    Re: Image file is blank?

    I thought you were manipulating the size and dimensions of the image.

    I was just saying if the image is a compressed image, changing the size, i.e. zooming or shrinking, may not make much difference in the physical size of the resulting file if you saved the image at the new size. The size of the image, i.e. its dimensions are changed but there isn't a direct correlation to the size of the resulting file when the file is compressed.

    For instance, I believe in Windows XP and Windows7 they used to have a couple of example jpg files in the Pictures\examples directory, one was named BlueHills.jpg (if I remember correctly), and the other Red...(sky or something) which both were fairly large in dimension, but very small in file size. I assume if you loaded the image in paint, shrunk it to 50% (so now it would be 1/4 the size or 25% of its former size in pixels) and then saved it as a new jpg file, the file size itself would not be that much smaller. Both files had a lot of shades of one color (blue in the one, and red in the other), so compressed quite a bit. Reducing the size of the drawing to one forth its size is not going to change how much compression you will get out of the blue and red pixels remaining compared to the original image.

    If you want to see a large change in size, save it in an uncompressed format. If you load your jpg file and save it as a bitmap you will get a huge file. If you then shrink it by 70% as you did and save it again as a bitmap, you will see the expected difference (a much smaller bitmap file) compared to the larger bitmap file.
    Of course it is quite likely that your small bitmap file is still larger than your large jpeg file.
    Last edited by passel; May 7th, 2018 at 03:41 PM.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,685

    Re: Image file is blank?

    Ok,
    I've attached a file in it's original size which is 349KB. I opened it in Paint and then "Resized" it using the Percentage option and chose 50 for Horizontal & Vertical. It resized it and when I saved it...the new size was 102KB. That's what I'm trying to accomplish.
    Attached Images Attached Images  
    Blake

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,403

    Re: Image file is blank?

    What type of file is it? If you don't attach the file as a file, perhaps in a zip, it is converted to a jpg by the forum and resized (usually) to be displayed on the post page, so I don't know what type it originally was or what its dimensions might have been.

    But, that doesn't matter since I decided to at least take another look at your latest code.

    You've loaded the original file into image.
    You've drawn image into newImage.
    You then save image which is your original picture that you loaded, so of course it is about the same size because it is the same bitmap, just gone through a decompress and recompress cycle.

    Since you've drawn image into newImage making it smaller, you want to save newImage.

    That is what you had in your original code.
    Code:
                    newPath = newPath + @"\" + filename + "rz.jpg";
                    newImage.Save(newPath);
    I didn't expect you to change that.

    It appears perhaps you don't know what the graphics object is doing for you.
    The graphics object is used to draw on something, i.e. a bitmap, image or other thing that allows a graphics object to draw on it
    .
    You get the graphics object with the following line.
    Graphics GFX = Graphics.FromImage((Bitmap)newImage)

    So, the GFX graphics object is used to draw on newImage.
    You could do GFX.DrawRectangle to draw a rectangle on newImage.
    You could do GFX.DrawLine to draw a line on newImage.
    In your case you're using GFX.DrawImage to draw an image on newImage. The image that you are drawing on newImage is the first argument passed as the first parameter to the DrawImage method, i.e. image.
    So, you've drawn the image named image in a smaller size to newImage. So, newImage is what you want to save.
    Last edited by passel; May 7th, 2018 at 06:09 PM.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,685

    Re: Image file is blank?

    I switched it back to the newImage.Save() but again, the size of the image only decreased by 5KB when I was trying to reduce it by 60%.
    Blake

  11. #11
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,258

    Re: Image file is blank?

    Among other things (resolutions, smoothing etc), you can use System.Drawing.Imaging.Encoder to set the quality of an Image (use a bitmap).
    I won't go into details but it seems that you are desperately want to reduces the size of the bytes of the image and not the size of the dimensions of the image (or both but you want the size-kb to be reduced). Of course lowering the KB will reduce the quality of the image.

    Edit:
    I think by default the jpg is saved at a 70% (meaning 30% reduction), so anything more you need to use on what I'm suggesting(or do, save by save by save but that is not a good practice)
    Last edited by sapator; May 8th, 2018 at 03:29 AM.
    Slow as hell.

  12. #12
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,258

    Re: Image file is blank?

    I've lowered you "cat" image quality form 65kb to 49kb using a 50% jpeg
    Name:  2.jpg
Views: 14
Size:  48.2 KB
    I've lowered your cat image to 32kb using 70% jpgeg. I think anything less starts to show the reduction lags on the image.
    Name:  3.jpg
Views: 14
Size:  31.3 KB
    Slow as hell.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,403

    Re: Image file is blank?

    Well, I'm not sure what else I can do.
    I created a new C# project (vs2010), and copied the bulk of the Form1.Designer.cs file into my Form1.Designer.cs file, and the bulk of Form1.cs into my Form1.cs (from your original project in post #1), so I had a runable version of the project with all the controls and methods. I then replaced the btnREsizeImages_Click method with the one you provided in post #4.
    I updated that method to do newImage.Save and newImage.Dispose instead of image.Save and image.Dispose.
    Code:
        private void btnResizeImages_Click(object sender, EventArgs e)
        {
          foreach (ListViewItem item in lv1.Items)
          {
            var filePath = item.Text;
            var newPath = txtNewImagePath.Text;
            var filename = Path.GetFileNameWithoutExtension(item.ToString());
            int imageWidth;
            int imageHeight;
            double pct = double.Parse(comboBox1.SelectedItem.ToString()) / 100;
    
            DirectoryInfo newDir = Directory.CreateDirectory(txtNewImagePath.Text);
    
            using (var image = Image.FromFile(filePath))
            {
              imageWidth = (int)(image.Width - ((int)image.Width * pct));
              imageHeight = (int)(image.Height - ((int)image.Height * pct));
    
              Size sz = new Size(imageWidth, imageHeight);
              Image newImage = new Bitmap(sz.Width, sz.Height);
    
              using (Graphics GFX = Graphics.FromImage((Bitmap)newImage))
              {
                GFX.DrawImage(image, new Rectangle(Point.Empty, sz));
              }
    
              newPath = newPath + @"\" + filename + "rz.jpg";
              newImage.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    
              lv2.Items.Add(newPath);
              newImage.Dispose();
            }
          }
        }
    I had copied your cat image from the post #8, pasted it in Paint and saved it as a jpeg file (cat600x491.jpg).
    From the forum, the dimensions ended up being 600x491, hence the name.
    The size of the file was 121KB.

    I ran the project, selected an output directory, dragged that image over to ListView1, selected 70% and hit the Resize button.
    I had a breakpoint in the btnResizeImages and I stepped through the code line by line looking at the values it calculated, and verified the size of the bitmap created (180 x 147) and it saved the image to the output directory I specified.
    The filename in that directory was cat600x491rz.jpg.
    The size of the file was 7KB.
    If I load the file in paint, or look at the "Details" tab of the file properties of the file, it shows that the image is indeed 180 x 147 pixels.

    So, the code changed the dimensions of the image, and the size of the file went from 121 KB to 7 KB. The code appears to be working fine, so I'm not sure where you may have gone wrong. Perhaps looking in a previous directory generated earlier and not the current directory you're outputting to. I don't know.
    It works fine for me, so no point in my trying to speculate any further.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width