Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Resizing without losing the pixel depth....

1 view
Skip to first unread message

Curt Fluegel

unread,
Nov 22, 2002, 6:34:46 PM11/22/02
to
Seems like a simple task, but it isn't working for me!

All I need to do is change the size of a bitmap. The source file in a PNG,
but I can make it anything if that is easier. The destination HAS to be an
8 bit PNG file of a given size. Can someone help me? The simple solutions
seems to change the pixel depth to 32 bit automatically....

Thanks,

Curt


Abraham Luna

unread,
Nov 22, 2002, 9:53:12 PM11/22/02
to
dim bmpLogo as new bitmap(320, 320, PixelFormat.Format8bppIndexed)

hope that helps

"Curt Fluegel" <cu...@midifarm.com> wrote in message
news:#sBmjBokCHA.1488@tkmsftngp11...

Curt Fluegel

unread,
Nov 23, 2002, 1:28:11 PM11/23/02
to
Thanks, but not really. I am well aware of how to create the bitmap,
but I need to resize an existing image off of the filesystem.
Obviously a little more is involved in this. I tried the following
that I found on the group:

Image originalimage = System.Drawing.Image.FromFile("c:\\input.jpeg");
Bitmap b=new Bitmap(originalimage,new Size(100,100));
b.Save("c:\\output.jpeg",ImageFormat.Jpeg);

Doing this I lose the original bit depth. I need to do exactly this,
but be able to save the new one as 8bit...

Curt

"Abraham Luna" <a...@attilanet.com> wrote in message news:<O$ahGKokCHA.440@tkmsftngp11>...

Michael Barnes

unread,
Nov 27, 2002, 2:56:11 AM11/27/02
to
Curt,

I cannot offer you any help on this - because I am having
the same problem. All I have discovered so far is that
GDI+ seems to throw an exception if you do anything that
involves changing pixeldepth downwards. I am trying to
output a monochrome TIFF image - 1bpp.

So far I have unsuccessfully tried:

Bitmap.Clone ( bounds, PixelFormat.Format1bppIndexed );
(throws an exception)

Creating a new bitmap and doing a SetPixel ( GetPixel )
loop (SetPixel throws an exception)

Saving the image with Image.Save and specifying a
ImagingCodecInfo with a ColorDepth parameter.
(throws an exception)

I am hunting for a solution; if I find one I'll be back to
let you know!

>.
>

james

unread,
Nov 27, 2002, 1:42:42 PM11/27/02
to
Here is a Save routine that I put together from several sources (any
recognizing thier code please forgive the mess it might have turned into )
Dim bm As New Bitmap(PictureBox1.Image)

Dim myX As Integer

Dim myY As Integer

myX = Val(txtX.Text)

myY = Val(txtY.Text)

If Val(txtY.Text) = 0 Or Val(txtX.Text) = 0 Then Exit Sub Else

Dim thumb As New Bitmap(myX, myY)

Dim g As Graphics = Graphics.FromImage(thumb)

g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

g.DrawImage(bm, New Rectangle(0, 0, myX, myY), New Rectangle(0, 0, bm.Width,
bm.Height), GraphicsUnit.Pixel)

g.Dispose()

Dim message, title, defaultValue As String

Dim myValue As String

message = "Enter File Name:" ' Set prompt.

title = "Save Resized Photo" ' Set title.

defaultValue = "" ' Set default value.

'get name to save resized image, using Inputbox

myValue = InputBox(message, title, defaultValue)

If myValue = "" Then bm.Dispose() : thumb.Dispose() : Exit Sub Else

thumb.Save("C:\" + myValue + ".jpg",
System.Drawing.Imaging.ImageFormat.Jpeg)

bm.Dispose()

thumb.Dispose()

Dim caption As String = "Save Resized Photo"

MessageBox.Show("The Resized Photo has been saved as: " & myValue, caption,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

****************************************************************************
*****

This seems to preserve the image quality when the image is downsized. And
up to a point even when it is enlarged.This line seems to help the most:

g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

Changing InterpolationMode to anything else produces a less than acceptable
image(for me anyway).

Hope this might help.

james

"Michael Barnes" <gen...@powerup.com.au> wrote in message
news:199a301c295f2$5741f5c0$89f82ecf@TK2MSFTNGXA01...
> Uh-Oh - reading article Q319591 seems to suggest that the
> answer is simply "You can't do it" :-( Not unless you are
> willing to directly manipulate the raster data and move it
> from one bitmap's buffer to anothers, transforming it
> along the way. I can't believe that is the only option!
> I will continue to search...


james

unread,
Nov 28, 2002, 9:56:06 AM11/28/02
to
Your right Michael, I did overlook that point. I have been working with
full color images and the code I posted deals with them just fine. (in other
words , my users cannot see any lose of image quality when the image is
downsized, which is what I was after).
It sounds to me like you will have to work directly with the api's in order
to achieve what you want to do.
Good luck.
james

"Michael Barnes" <gen...@powerup.com.au> wrote in message

news:1bfdb01c296a7$b3a90aa0$8af82ecf@TK2MSFTNGXA03...
> Thanks, James, but I think you missed a critical point; we
> are not having any problem resizing the image; the problem
> is retaining the original colour depth, if it was
> something less than full-colour. For example, try taking
> a monochrome (1bpp) TIFF file, adding borders to it, then
> save it again as a 1bpp TIFF file. I'd be interested to
> see the code that can do it!
>


Curt Fluegel

unread,
Nov 30, 2002, 3:37:50 PM11/30/02
to
I ended up purchasing a COM object that did it just fine. Perhaps
some later version of .NET will give us what we need... Until then,
dll hell for me...

Curt
midiringtones.com


"james" <jjam...@earthlink.net> wrote in message news:<#86Rq3ulCHA.2644@tkmsftngp02>...

B-MAN

unread,
Dec 3, 2002, 8:07:58 PM12/3/02
to
I ran into a similiar prolem as you have. I read in a 1bpp tiff image
however when I snag it's hBitmap GDI+ auto converts it to 32bpp. I need to
draw some text on the loaded image (overlay) and write it back out as a
group 4 tiff. However group 4 compression is unavailable in any bit depth
other than 1. So I fought that problem for quite a while and came up with a
solution that seems to work for me and is a ton faster than using lockbits
in GDI+. Since my text rendering was being done in GDI rather than GDI+ to
gain some significant rendering speed and the fact that the second you touch
a GDI+ bitmap it's converted to 32bpp in the background I wrote the
conversion routine in GDI using DIBSections. Below is some sample code I
used to develop my function and the makemono function as well. I'm open to
any improvements or flaws in my logic anyone sees that I missed. I havn't
had the chance yet to really test the makemono function with inputs other
than the mono tiff I load as my image overlay. I was able to significantly
reduce my image rendering time this way. Using GDI+ Lockbits() would take
approximately 3 or 4 minutes to do the monocrome conversion (per image),
however using the below code and rendering my text in GDI, I was able to
speed up my rendering to around 17 images a second. A bit of a performance
gain <g>. My reference system is a Toshiba Laptop, P4 1 Ghz, 256 MB RAM.

Anyhow, I hope this helps with your attempts to save a 1bpp Tiff image.
Although, I'd be interested in hearing the results if someone mod'ed the
makemono function to output other bit depths by changing the commented line
to their desired bit depth.

Barry Lance


-- Sample VB.NET Code --

Sub Main()


Dim srcBmp As Bitmap = Bitmap.FromFile("C:\dwimage.Tif") ' Load my
1bpp image
Dim hsrcBitmap As Integer = srcBmp.GetHbitmap.ToInt32 ' Bitmap
has just been made 32bpp
Dim hdstBitmap As Integer = MakeMonoDIBitmap(hsrcBitmap) ' Convert
it back to 1bpp

' Make a new GDI+ bitmap with my 1bpp DI Bitmap
Dim dstBmp As Bitmap =
Bitmap.FromHbitmap(IntPtr.op_Explicit(hdstBitmap))

Dim EncParms As Imaging.EncoderParameters
Dim parmComp As Imaging.EncoderParameter

EncParms = New Imaging.EncoderParameters(1)

parmComp = New
Imaging.EncoderParameter(Imaging.Encoder.Compression,Imaging.EncoderValue.CompressionCCITT4)
EncParms.Param.SetValue(parmComp, 0)

try

dstBmp.Save("c:\DIBTest.tif", GetEncoderInfo("image/tiff"), EncParms)

Catch e As Exception ' Doh!!! Image isn't 1bpp for
some reason so we'll just save using RLE compression

dstBmp.Save("c:\DIBTestRLE.tif", Imaging.ImageFormat.Tiff)

End Try


DeleteObject(hsrcBitmap)
DeleteObject(hdstBitmap)

End Sub

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

Public Function MakeMonoDIBitmap(ByVal hbitmap As Integer) As Integer

' Function takes a GDI HBITMAP object and returns a GDI DIB Section
' of the same size however the pixel depth is reduced if necessary
to 1.
' The DIB section can be used to create a GDI+ bitmap object
' with system.drawing.bitmap.fromhbitmap or selected into a DC and
' drawn upon with the various GDI routines.

' This function was writtin with the assumption that the souce
HBITMAP
' isn't a color bitmap (i.e. monocrome). I havn't tested this
routine
' using a color image as an input.
'
' Also the caller is responsible for calling DeleteObject on the
returned DIB Section
'
' Barry M Lance


Dim rcode As Integer
Dim srcInfo As GDIBITMAP

Dim ScreenDC As Integer = CreateDC("DISPLAY", "", "", 0)

Dim srcDC As Integer = CreateCompatibleDC(ScreenDC)
Dim dstDC As Integer = CreateCompatibleDC(ScreenDC)

Dim srcDIBitmap, dstDIBitmap As Integer
Dim srcOldBitmap, dstOldBitmap As Integer

Dim srcBMI, dstBMI As BITMAPINFOHEADER
Dim srcBits, dstBits As Integer

rcode = GetBitmapObject(hbitmap, 24, srcInfo)

With srcBMI

.biSize = 40
.biWidth = srcInfo.bmWidth
.biHeight = srcInfo.bmHeight
.biPlanes = srcInfo.bmPlanes
.biBitCount = .biPlanes * srcInfo.bmBitsPixel
.biCompression = BI_RGB
.biCompression = 0
.biSizeImage = 0
.biXPelsPerMeter = 0
.biYPelsPerMeter = 0
.biClrUsed = 0
.biClrImportant = 0


End With

dstBMI = srcBMI
dstBMI..biBitCount = 1 ' This is our desired bitdepth (Should be
.biPlanes * bpp)


srcDIBitmap = CreateDIBSection(0, srcBMI, 0, srcBits, 0, 0)
rcode = GetDIBits(srcDC, hbitmap, 0, srcBMI.biHeight, srcBits,
srcBMI, DIB_PAL_COLORS)
srcOldBitmap = SelectObject(srcDC, srcDIBitmap)

dstDIBitmap = CreateDIBSection(0, dstBMI, 0, dstBits, 0, 0)
dstOldBitmap = SelectObject(dstDC, dstDIBitmap)

' Bitblt is supposed to be faster than SetDIBits so I use it instead (it
will do the bit depth reduction on a DIB for me)
rcode = BitBlt(dstDC, 0, 0, dstBMI.biWidth, dstBMI.biHeight, srcDC,
0, 0, SRCCOPY)

SelectObject(srcDC, srcOldBitmap)
SelectObject(dstDC, dstOldBitmap)

DeleteObject(srcDIBitmap)

DeleteDC(srcDC)
DeleteDC(dstDC)
DeleteDC(ScreenDC)

MakeMonoDIBitmap = dstDIBitmap

End Function

0 new messages