drawing image as a partly transparent one

B

Brian Henry

I have an icon I want to draw onto the screen, but I want to streatch it out
to be about 256x256 and make it about 75% transparent how would I go about
this? do i need to convert it to a bitmap first then paint it larger? and
how would i make the entire image transparent at 75% transparency (25%
opaque) thanks!
 
H

Herfried K. Wagner [MVP]

* "Brian Henry said:
I have an icon I want to draw onto the screen, but I want to streatch it out
to be about 256x256 and make it about 75% transparent how would I go about
this? do i need to convert it to a bitmap first then paint it larger? and
how would i make the entire image transparent at 75% transparency (25%
opaque) thanks!

Quick and Dirty:

\\\
Imports System.Drawing
Imports System.Drawing.Imaging
..
..
..
Dim bo1 As Bitmap = _
DirectCast(Image.FromFile("C:\WINDOWS\ANGLER.BMP"), Bitmap)
Dim bo2 As Bitmap = _
DirectCast(Image.FromFile("C:\WINDOWS\FÄCHER.BMP"), Bitmap)
Dim bt1 As Bitmap = _
New Bitmap(bo1.Width, bo1.Height, PixelFormat.Format32bppArgb)
Dim bt2 As Bitmap = _
New Bitmap(bo2.Width, bo2.Height, PixelFormat.Format32bppArgb)
Dim g1 As Graphics = Graphics.FromImage(bt1)
Dim g2 As Graphics = Graphics.FromImage(bt2)
g1.DrawImage(bo1, 0, 0)
g2.DrawImage(bo2, 0, 0)
g1.Dispose()
g2.Dispose()
Dim i As Integer, j As Integer
For i = 0 To bt1.Width - 1
For j = 0 To bt1.Height - 1
bt1.SetPixel(i, j, Color.FromArgb(120, bt1.GetPixel(i, j)))
Next j
Next i
g1 = Graphics.FromImage(bt2)
g1.DrawImage(bt1, 0, 0)
g1.Dispose()
Me.BackgroundImage = bt2
bt1.Dispose()
bo1.Dispose()
bo2.Dispose()
///
 
B

Brian Henry

ick, I was hopeing to go the rought of not manuall changeing the alpha
channel pixel by pixel.. I thought I saw in one of my .NET books a function
to draw a image partly transparent with out doing this.
 
H

Herfried K. Wagner [MVP]

* "Brian Henry said:
ick, I was hopeing to go the rought of not manuall changeing the alpha
channel pixel by pixel.. I thought I saw in one of my .NET books a function
to draw a image partly transparent with out doing this.

Maybe using a 'ColorMatrix' will have a better performance (untested!):

<URL:http://www.google.de/[email protected]>
 
B

Brian Henry

I think this may do it too... I looked it up in my .NET book and it says
this example:

' Create a clone bithman, and make it transparent
dim bmp as new bitmap("c:\image.bmp")
' define transparency level
dim transparency as single = 0.8
' create 5x5 matric with transparency val in position (4,4)
dim values()() as single = {new single(){1,0,0,0,0}, _
new single() {0,1,0,0,0}, _
new single() {0,0,1,0,0}, _
new single() {0,0,0,transparency,0}, _
new single() {0,0,0,0,1}}
' use matrix to initialize new color matrix object
dim colMatrix as new ColorMatrix(values)
' create an imageattribute object, and assign its color matrix
dim imgAttr as new imageattributes()
imgAttr.SetColorMatrix(colMatrix,ColorMatrixFlag.Default _
colorAdjustType.Bitmap)
'draw the bitmap using specificed image attributes
gr.drawImage(bmp,new
rectangle(200,20,bmp.width,bmp.height),0,0,bmp.width,bmp.height, _
graphicunit.pixel,imageAttr)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top