[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

How to use GetThumbnailImage without embedded thumbnail?

George Nevsky

12/21/2004 3:35:00 PM

I wish to use GetThumbnailImage method of Image object to downsize an image
but I don't want to use embedded thumbnail which can be in original image
file.



I know probably it isn't best in the world way but I like it. It works
absolutely fine for my purposes. It is very simple. It works fine with
indexed color images like GIF as well as with true color images like JPEG.
It is pretty fast. But it has one horrible thing. It is using embedded
thumbnail (if there is one) which can be ugly.

How to tell to GDI+ don't use embedded thumbnail?


9 Answers

Bob Powell

12/21/2004 3:39:00 PM

0

Just create your thumbnail manually by creating an image the size of the
desired thumbnail, obtaining a Graphics object from it using
Graphics.FromImage and drawing the original image to the thumbnail at the
appropriate size.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"George Nevsky" <apokriffree@hotbox.ru> wrote in message
news:ONYhvK35EHA.3124@TK2MSFTNGP11.phx.gbl...
> I wish to use GetThumbnailImage method of Image object to downsize an
image
> but I don't want to use embedded thumbnail which can be in original image
> file.
>
>
>
> I know probably it isn't best in the world way but I like it. It works
> absolutely fine for my purposes. It is very simple. It works fine with
> indexed color images like GIF as well as with true color images like JPEG.
> It is pretty fast. But it has one horrible thing. It is using embedded
> thumbnail (if there is one) which can be ugly.
>
> How to tell to GDI+ don't use embedded thumbnail?
>
>


George Nevsky

12/22/2004 6:23:00 AM

0

But Graphics.FromImage doesn't work with indexed color pictures ...


"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OCu3TN35EHA.1564@TK2MSFTNGP09.phx.gbl...
> Just create your thumbnail manually by creating an image the size of the
> desired thumbnail, obtaining a Graphics object from it using
> Graphics.FromImage and drawing the original image to the thumbnail at the
> appropriate size.
>
> --
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Find great Windows Forms articles in Windows Forms Tips and Tricks
> http://www.bobpowell.net/tips...
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/f...
>
> All new articles provide code in C# and VB.NET.
> Subscribe to the RSS feeds provided and never miss a new article.
>
>
>
>
>
> "George Nevsky" <apokriffree@hotbox.ru> wrote in message
> news:ONYhvK35EHA.3124@TK2MSFTNGP11.phx.gbl...
>> I wish to use GetThumbnailImage method of Image object to downsize an
> image
>> but I don't want to use embedded thumbnail which can be in original image
>> file.
>>
>>
>>
>> I know probably it isn't best in the world way but I like it. It works
>> absolutely fine for my purposes. It is very simple. It works fine with
>> indexed color images like GIF as well as with true color images like
>> JPEG.
>> It is pretty fast. But it has one horrible thing. It is using embedded
>> thumbnail (if there is one) which can be ugly.
>>
>> How to tell to GDI+ don't use embedded thumbnail?
>>
>>
>
>


(Remy Samulski)

12/22/2004 7:38:00 AM

0

I use the following code to create thumbnails and to enlarge images.

Public Shared Function ResizeBitmap( _
ByVal bmpBitmap As Bitmap, _
ByVal snglBoundingWidth As Single, _
ByVal snglBoundingHeight As Single, _
Optional ByVal blnHighQuality As Boolean = False) As Bitmap
Dim snglNewWidth, snglNewHeight As Single

If snglBoundingWidth > 1 And snglBoundingHeight > 1 Then
If (bmpBitmap.Height / bmpBitmap.Width) >
(snglBoundingHeight / snglBoundingWidth) Then
snglNewHeight = Int(snglBoundingHeight)
snglNewWidth = Int((bmpBitmap.Width / bmpBitmap.Height)
* snglNewHeight)
Else
snglNewWidth = Int(snglBoundingWidth)
snglNewHeight = Int((bmpBitmap.Height /
bmpBitmap.Width) * snglNewWidth)
End If
Else
snglNewWidth = bmpBitmap.Width * snglBoundingWidth
snglNewHeight = bmpBitmap.Height * snglBoundingHeight
End If

Dim bmpNewBitmap As System.Drawing.Bitmap

bmpNewBitmap = New Bitmap(bmpBitmap, snglNewWidth,
snglNewHeight)

If blnHighQuality = True Then
Dim g As Graphics = Graphics.FromImage(bmpNewBitmap)

g.InterpolationMode =
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

g.DrawImage(bmpBitmap, 0, 0, bmpNewBitmap.Width,
bmpNewBitmap.Height)
End If

Return bmpNewBitmap
End Function

Bob Powell

12/22/2004 11:35:00 AM

0

It's not the indexed image that you should use.

Image i=Image.FromFile(<original>);
Bitmap bm=new Bitmap(thumbXsize, thumbYsize);
Graphics g=Graphics.FromImage(bm);
g.InterpolationMode=InterpolationMode.HighQualityBilinear;
g.DrawImage(i,new Rectangle(0,0,bm.Width,bm.Height),0,0,i.Width,
i.Height,GraphicsUnit.pixel);
g.Dispose();
i.Dispose();

bm now holds a thumbnail image that you can save or display;
--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"George Nevsky" <apokriffree@hotbox.ru> wrote in message
news:%23iy686%235EHA.208@TK2MSFTNGP12.phx.gbl...
> But Graphics.FromImage doesn't work with indexed color pictures ...
>
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:OCu3TN35EHA.1564@TK2MSFTNGP09.phx.gbl...
> > Just create your thumbnail manually by creating an image the size of the
> > desired thumbnail, obtaining a Graphics object from it using
> > Graphics.FromImage and drawing the original image to the thumbnail at
the
> > appropriate size.
> >
> > --
> > Bob Powell [MVP]
> > Visual C#, System.Drawing
> >
> > Find great Windows Forms articles in Windows Forms Tips and Tricks
> > http://www.bobpowell.net/tips...
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/f...
> >
> > All new articles provide code in C# and VB.NET.
> > Subscribe to the RSS feeds provided and never miss a new article.
> >
> >
> >
> >
> >
> > "George Nevsky" <apokriffree@hotbox.ru> wrote in message
> > news:ONYhvK35EHA.3124@TK2MSFTNGP11.phx.gbl...
> >> I wish to use GetThumbnailImage method of Image object to downsize an
> > image
> >> but I don't want to use embedded thumbnail which can be in original
image
> >> file.
> >>
> >>
> >>
> >> I know probably it isn't best in the world way but I like it. It works
> >> absolutely fine for my purposes. It is very simple. It works fine with
> >> indexed color images like GIF as well as with true color images like
> >> JPEG.
> >> It is pretty fast. But it has one horrible thing. It is using embedded
> >> thumbnail (if there is one) which can be ugly.
> >>
> >> How to tell to GDI+ don't use embedded thumbnail?
> >>
> >>
> >
> >
>
>


George Nevsky

12/22/2004 2:52:00 PM

0

Have you tried resize indexed color image (GIF) with your function?
This line:
> Dim g As Graphics = Graphics.FromImage(bmpNewBitmap)
should generate exception "A Graphics object cannot be created from an image
that has an indexed pixel format."


"Rémy Samulski" <samulski@hotmail.com> wrote in message
news:1103701069.808543.160590@f14g2000cwb.googlegroups.com...
>I use the following code to create thumbnails and to enlarge images.
>
> Public Shared Function ResizeBitmap( _
> ByVal bmpBitmap As Bitmap, _
> ByVal snglBoundingWidth As Single, _
> ByVal snglBoundingHeight As Single, _
> Optional ByVal blnHighQuality As Boolean = False) As Bitmap
> Dim snglNewWidth, snglNewHeight As Single
>
> If snglBoundingWidth > 1 And snglBoundingHeight > 1 Then
> If (bmpBitmap.Height / bmpBitmap.Width) >
> (snglBoundingHeight / snglBoundingWidth) Then
> snglNewHeight = Int(snglBoundingHeight)
> snglNewWidth = Int((bmpBitmap.Width / bmpBitmap.Height)
> * snglNewHeight)
> Else
> snglNewWidth = Int(snglBoundingWidth)
> snglNewHeight = Int((bmpBitmap.Height /
> bmpBitmap.Width) * snglNewWidth)
> End If
> Else
> snglNewWidth = bmpBitmap.Width * snglBoundingWidth
> snglNewHeight = bmpBitmap.Height * snglBoundingHeight
> End If
>
> Dim bmpNewBitmap As System.Drawing.Bitmap
>
> bmpNewBitmap = New Bitmap(bmpBitmap, snglNewWidth,
> snglNewHeight)
>
> If blnHighQuality = True Then
> Dim g As Graphics = Graphics.FromImage(bmpNewBitmap)
>
> g.InterpolationMode =
> Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
>
> g.DrawImage(bmpBitmap, 0, 0, bmpNewBitmap.Width,
> bmpNewBitmap.Height)
> End If
>
> Return bmpNewBitmap
> End Function
>


George Nevsky

12/22/2004 7:15:00 PM

0

Ok. Thank you for this example but can you also help me with indexed color
image?
How to create Graphics object from GIF?

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:ehU4OpB6EHA.3856@tk2msftngp13.phx.gbl...
> It's not the indexed image that you should use.
>
> Image i=Image.FromFile(<original>);
> Bitmap bm=new Bitmap(thumbXsize, thumbYsize);
> Graphics g=Graphics.FromImage(bm);
> g.InterpolationMode=InterpolationMode.HighQualityBilinear;
> g.DrawImage(i,new Rectangle(0,0,bm.Width,bm.Height),0,0,i.Width,
> i.Height,GraphicsUnit.pixel);
> g.Dispose();
> i.Dispose();
>
> bm now holds a thumbnail image that you can save or display;
> --
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Find great Windows Forms articles in Windows Forms Tips and Tricks
> http://www.bobpowell.net/tips...
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/f...
>
> All new articles provide code in C# and VB.NET.
> Subscribe to the RSS feeds provided and never miss a new article.
>
>
>
>
>
> "George Nevsky" <apokriffree@hotbox.ru> wrote in message
> news:%23iy686%235EHA.208@TK2MSFTNGP12.phx.gbl...
>> But Graphics.FromImage doesn't work with indexed color pictures ...
>>
>>
>> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
>> news:OCu3TN35EHA.1564@TK2MSFTNGP09.phx.gbl...
>> > Just create your thumbnail manually by creating an image the size of
>> > the
>> > desired thumbnail, obtaining a Graphics object from it using
>> > Graphics.FromImage and drawing the original image to the thumbnail at
> the
>> > appropriate size.
>> >
>> > --
>> > Bob Powell [MVP]
>> > Visual C#, System.Drawing
>> >
>> > Find great Windows Forms articles in Windows Forms Tips and Tricks
>> > http://www.bobpowell.net/tips...
>> >
>> > Answer those GDI+ questions with the GDI+ FAQ
>> > http://www.bobpowell.net/f...
>> >
>> > All new articles provide code in C# and VB.NET.
>> > Subscribe to the RSS feeds provided and never miss a new article.
>> >
>> >
>> >
>> >
>> >
>> > "George Nevsky" <apokriffree@hotbox.ru> wrote in message
>> > news:ONYhvK35EHA.3124@TK2MSFTNGP11.phx.gbl...
>> >> I wish to use GetThumbnailImage method of Image object to downsize an
>> > image
>> >> but I don't want to use embedded thumbnail which can be in original
> image
>> >> file.
>> >>
>> >>
>> >>
>> >> I know probably it isn't best in the world way but I like it. It works
>> >> absolutely fine for my purposes. It is very simple. It works fine with
>> >> indexed color images like GIF as well as with true color images like
>> >> JPEG.
>> >> It is pretty fast. But it has one horrible thing. It is using embedded
>> >> thumbnail (if there is one) which can be ugly.
>> >>
>> >> How to tell to GDI+ don't use embedded thumbnail?
>> >>
>> >>
>> >
>> >
>>
>>
>
>


Bob Powell

12/22/2004 8:20:00 PM

0

You cannot create a Graphics object from a GIF.

The resulting thumbnail will always be a high colour image best saved as a
jpeg.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"George Nevsky" <apokriffree@hotbox.ru> wrote in message
news:e6%23IVqF6EHA.4040@TK2MSFTNGP14.phx.gbl...
> Ok. Thank you for this example but can you also help me with indexed color
> image?
> How to create Graphics object from GIF?
>
> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> news:ehU4OpB6EHA.3856@tk2msftngp13.phx.gbl...
> > It's not the indexed image that you should use.
> >
> > Image i=Image.FromFile(<original>);
> > Bitmap bm=new Bitmap(thumbXsize, thumbYsize);
> > Graphics g=Graphics.FromImage(bm);
> > g.InterpolationMode=InterpolationMode.HighQualityBilinear;
> > g.DrawImage(i,new Rectangle(0,0,bm.Width,bm.Height),0,0,i.Width,
> > i.Height,GraphicsUnit.pixel);
> > g.Dispose();
> > i.Dispose();
> >
> > bm now holds a thumbnail image that you can save or display;
> > --
> > Bob Powell [MVP]
> > Visual C#, System.Drawing
> >
> > Find great Windows Forms articles in Windows Forms Tips and Tricks
> > http://www.bobpowell.net/tips...
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/f...
> >
> > All new articles provide code in C# and VB.NET.
> > Subscribe to the RSS feeds provided and never miss a new article.
> >
> >
> >
> >
> >
> > "George Nevsky" <apokriffree@hotbox.ru> wrote in message
> > news:%23iy686%235EHA.208@TK2MSFTNGP12.phx.gbl...
> >> But Graphics.FromImage doesn't work with indexed color pictures ...
> >>
> >>
> >> "Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
> >> news:OCu3TN35EHA.1564@TK2MSFTNGP09.phx.gbl...
> >> > Just create your thumbnail manually by creating an image the size of
> >> > the
> >> > desired thumbnail, obtaining a Graphics object from it using
> >> > Graphics.FromImage and drawing the original image to the thumbnail at
> > the
> >> > appropriate size.
> >> >
> >> > --
> >> > Bob Powell [MVP]
> >> > Visual C#, System.Drawing
> >> >
> >> > Find great Windows Forms articles in Windows Forms Tips and Tricks
> >> > http://www.bobpowell.net/tips...
> >> >
> >> > Answer those GDI+ questions with the GDI+ FAQ
> >> > http://www.bobpowell.net/f...
> >> >
> >> > All new articles provide code in C# and VB.NET.
> >> > Subscribe to the RSS feeds provided and never miss a new article.
> >> >
> >> >
> >> >
> >> >
> >> >
> >> > "George Nevsky" <apokriffree@hotbox.ru> wrote in message
> >> > news:ONYhvK35EHA.3124@TK2MSFTNGP11.phx.gbl...
> >> >> I wish to use GetThumbnailImage method of Image object to downsize
an
> >> > image
> >> >> but I don't want to use embedded thumbnail which can be in original
> > image
> >> >> file.
> >> >>
> >> >>
> >> >>
> >> >> I know probably it isn't best in the world way but I like it. It
works
> >> >> absolutely fine for my purposes. It is very simple. It works fine
with
> >> >> indexed color images like GIF as well as with true color images like
> >> >> JPEG.
> >> >> It is pretty fast. But it has one horrible thing. It is using
embedded
> >> >> thumbnail (if there is one) which can be ugly.
> >> >>
> >> >> How to tell to GDI+ don't use embedded thumbnail?
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>
> >
> >
>
>


Bob Powell

12/22/2004 8:23:00 PM

0

You will see in both my example and Remy's that the bitmap for which the
Graphics object is obtained is a new high-colour bitmap. The bitmap creation
in Remy's example might cause problems though. You should just use new
Bitmap(snglNewWidth,snglNewHeight)

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tips...

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/f...

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"George Nevsky" <apokriffree@hotbox.ru> wrote in message
news:O6I8LXD6EHA.3944@TK2MSFTNGP12.phx.gbl...
> Have you tried resize indexed color image (GIF) with your function?
> This line:
> > Dim g As Graphics = Graphics.FromImage(bmpNewBitmap)
> should generate exception "A Graphics object cannot be created from an
image
> that has an indexed pixel format."
>
>
> "Rémy Samulski" <samulski@hotmail.com> wrote in message
> news:1103701069.808543.160590@f14g2000cwb.googlegroups.com...
> >I use the following code to create thumbnails and to enlarge images.
> >
> > Public Shared Function ResizeBitmap( _
> > ByVal bmpBitmap As Bitmap, _
> > ByVal snglBoundingWidth As Single, _
> > ByVal snglBoundingHeight As Single, _
> > Optional ByVal blnHighQuality As Boolean = False) As Bitmap
> > Dim snglNewWidth, snglNewHeight As Single
> >
> > If snglBoundingWidth > 1 And snglBoundingHeight > 1 Then
> > If (bmpBitmap.Height / bmpBitmap.Width) >
> > (snglBoundingHeight / snglBoundingWidth) Then
> > snglNewHeight = Int(snglBoundingHeight)
> > snglNewWidth = Int((bmpBitmap.Width / bmpBitmap.Height)
> > * snglNewHeight)
> > Else
> > snglNewWidth = Int(snglBoundingWidth)
> > snglNewHeight = Int((bmpBitmap.Height /
> > bmpBitmap.Width) * snglNewWidth)
> > End If
> > Else
> > snglNewWidth = bmpBitmap.Width * snglBoundingWidth
> > snglNewHeight = bmpBitmap.Height * snglBoundingHeight
> > End If
> >
> > Dim bmpNewBitmap As System.Drawing.Bitmap
> >
> > bmpNewBitmap = New Bitmap(bmpBitmap, snglNewWidth,
> > snglNewHeight)
> >
> > If blnHighQuality = True Then
> > Dim g As Graphics = Graphics.FromImage(bmpNewBitmap)
> >
> > g.InterpolationMode =
> > Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
> >
> > g.DrawImage(bmpBitmap, 0, 0, bmpNewBitmap.Width,
> > bmpNewBitmap.Height)
> > End If
> >
> > Return bmpNewBitmap
> > End Function
> >
>
>


(Remy Samulski)

12/28/2004 12:00:00 PM

0

No problems resizing GIF's nor with the function I use in my example.
The only thing you should take into consideration is that the bitmap is
resized without any anti-aliases when performing this code:
ResizedBitmap = new Bitmap(OriginalBitmap, snglNewWidth, snglNewHeight)
Furthermore my code is not speed optimised =) (as always).

Bob Powell [MVP] wrote:
> The bitmap creation
> in Remy's example might cause problems though. You should just use
new
> Bitmap(snglNewWidth,snglNewHeight)