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
hope that helps
"Curt Fluegel" <cu...@midifarm.com> wrote in message
news:#sBmjBokCHA.1488@tkmsftngp11...
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>...
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!
>.
>
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...
"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!
>
"james" <jjam...@earthlink.net> wrote in message news:<#86Rq3ulCHA.2644@tkmsftngp02>...
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