Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

Ok I have an image, that is out of proportion to what I want. I want to resize that image, WITHOUT compressing it again, losing file size.

 

Currently when I do this:

 

                   Dim oImg As Image = Image.FromFile("C:\sourcefile.jpg")
                   Dim oThumbNail As Image = New Bitmap(640, 480, oImg.PixelFormat)
                   Dim oGraphic As Graphics = Graphics.FromImage(oThumbNail)

                   oGraphic.CompositingMode = Drawing2D.CompositingMode.SourceOver
                   oGraphic.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                   oGraphic.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                   oGraphic.InterpolationMode = Drawing2D.InterpolationMode.Bicubic
                   oGraphic.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality

                   Dim oRectangle As Rectangle = New Rectangle(0, 0, 640, 480)
                   oGraphic.DrawImage(oImg, oRectangle)

                   oThumbNail.Save("C:\test.jpg", Imaging.ImageFormat.Jpeg)

 

The image seems to look the same but it compresses it. When I run that over an image that is already a 640x480 jpeg, @ 126 KB, it reduces it to 36KB

 

So I wasnt happy with that and did this:

 

 

                   Dim myImageCodecInfo As Imaging.ImageCodecInfo
                   Dim myEncoder As Imaging.Encoder
                   Dim myEncoderParameter As Imaging.EncoderParameter
                   Dim myEncoderParameters As Imaging.EncoderParameters
                   myImageCodecInfo = GetEncoderInfo("image/jpeg")

                   myEncoder = Imaging.Encoder.Compression
                   myEncoderParameters = New Imaging.EncoderParameters(1)

                   myEncoderParameter = New Imaging.EncoderParameter(myEncoder, Imaging.EncoderValue.CompressionNone)

                   myEncoderParameters.Param(0) = myEncoderParameter

Dim saveImage As Image = Image.FromFile("C:\sourcefile.jpg")
saveImage.Save("C:\test.jpg", myImageCodecInfo, myEncoderParameters)

 

So when I do this, it spits out an image about the same size, generally a little bigger, thats great, but I cant work resizing into that without it ignoring the encoder stuff and giving me a tiny little file again.

 

Any help is appreciated!!!!!!!!!!!!!!!!!!!

 

Thanks

Joshua

Posted
I could be wrong, but surely the image is always going to be of a smaller file size, since if you reduce the resolution of the image, there will be less pixels, and thus less pixel data stored in the file.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

Compresssion

 

Hi,

 

When the file is smaller, there should be some reduction in size, however as I said in my explanation, I am resizing a 640x480 image to 640x480, then saving it, and it decreases in size, which it shouldnt?

 

Not by 75% anyways.

  • Administrators
Posted

Jpeg images will alway suffer some compression when saving - they are a 'lossy' format. Even if you say try to save with no compression there will always be some loss in quality / file size.

You may be better of looking at something like .png as a file format as this does allow images to be saved with no loss of information.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

reply

 

hi,

 

I understand this, but when I use the encoder I can save it without loss of file size, but I cannot modify the size using this, thats the only thing extra I want to do, "some loss" is acceptable, but making a 120kb file 27kb when there has been no decrease in dimensions is not ok.

  • 2 weeks later...
Posted

Sounds's like alot of pixel data is being lost somewhere, does the second image look different than the first image?

 

It could just be a simple change in JPEG compression settings.

 

JPEG's lose pixel data the more they are transfered and/or modified, thus reducing quality and overall size of the file.

 

JPEG is a graphical compression standard for images, so anytime you re-save the same image data/bits you are re-compressing it.

 

In your example:

Dim saveImage As Image = Image.FromFile("C:\sourcefile.jpg")

saveImage.Save("C:\test.jpg", myImageCodecInfo, myEncoderParameters)

 

You are loading an already compressed JPEG image file, then you are re-saving it to another file thus re-saving the data and thus losing pixel data/bits. :-\

Posted

it doesnt matter, if you actually tried the code as supplie dyou will see that when you DO NOT change the dimensions, you can save out a jpeg the does not decrease in size, otherwise a compression about a 1meg file to a 60kb file is obtained.

 

yes it already is compressed, but thats not the issue

Posted

out of proportion to what I want. I want to resize that image, WITHOUT compressing it again ....

 

I'm new at this image thing but I wanted to proprotion an image without compressing it and this is what I did..actually someone with more image experience suggested this...

 

---

In my case, my image size requirement is 700x500 but some images are larger than 700x500..so I needed to reduce the image but make sure the proportion doesnt mess up (compress) the image..so this is what I did..I compressed the image using Thumbnail and then did the image proportion...this is my code

 

My Tiff image is 2179x1265 and this code changes it to 700x406...

 

Not sure if this is your issue but hope this helps

 


Dim filetoconvert As String
       filetoconvert = "c:\IP\newimage"

       Dim fullsizeimage As System.Drawing.Image
       fullsizeimage = System.Drawing.Image.FromFile("c:\IP\W0008-01-01.tif")

       '------------------------ Do the proportion
       Dim newwidth = fullsizeimage.Width
       Dim newHeight = fullsizeimage.Height

       If newwidth > 700 Then
           newHeight = newHeight * 700 / newwidth
           newwidth = 700
       End If

       If newHeight > 500 Then
           newwidth = newwidth * 500 / newHeight
           newHeight = 500
       End If


       '-----------------------------


       Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort

       dummyCallBack = New _
                       System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

       Dim thumbNailImg As System.Drawing.Image
              thumbNailImg = fullsizeimage.GetThumbnailImage(newwidth, newHeight, dummyCallBack, IntPtr.Zero)

       thumbNailImg.Save(filetoconvert & ".jpeg", ImageFormat.Jpeg)

       thumbNailImg.Dispose()

  • 1 year later...
Posted

Hi,

 

I have been trying to use the compression code supplied by Joshua, the image that I am using is 229 by 335 - I have adjusted the rectange and bitmap size properties to faciliated these sizes. When I run the code, all goes well until I get to the oThumbNail.Save line where I get the following error: "A generic error occured in GDI+". I am saving to a different file name and saving to my C drive.

 

The following is displayed in the browser after the throw exception is called:

Offending URL: [url]http://localhost/webAppFTP/WebForm8.aspx[/url]
Source: System.Web
Message: Exception of type System.Web.HttpUnhandledException was thrown.
Stack trace: at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain() at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

 

Any suggestions on what can cause this problem?

 

Mike55

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...