Self Draw a Image Button

M

Mike Malone

I need to dynamically create a bitmap (dynamic text) and then output it as the image on a image button.

I can create the bitmap fine and if I output it directly to the output stream all works just fine.

The problem comes in outputting during the render function of the control.

Do I need some html tag to state that the following output stream is really a bitmap stream instead of text?

- Mike
 
V

vMike

You just set the src to yourimagefile.aspx and use yourimage.Save(Response.OutputStream, ImageFormat.Jpeg)
You should also have a Response.ContentType = "image/jpeg" or netscape probably won't interpret it properly.
To draw text try something like the following

Function mgDrawText(objBitmap as bitmap, strText as string, intFontSize as int32, sysColor as color, intHorzPos as int32, intVertPos as int32) as bitmap
dim objGraphic as Graphics = Graphics.FromImage(objBitmap)
objGraphic.DrawString(strText,New Font("Tahoma",intFontSize),New SolidBrush(sysColor),intHorzPos, intVertPos)
return objBitMap
End Function
I need to dynamically create a bitmap (dynamic text) and then output it as the image on a image button.

I can create the bitmap fine and if I output it directly to the output stream all works just fine.

The problem comes in outputting during the render function of the control.

Do I need some html tag to state that the following output stream is really a bitmap stream instead of text?

- Mike
 
M

Mike Malone

I've looked at this possibility.

These are dynamically created buttons of varying text. I can dynamically create the button aspx file but, wont I run into a problem when multiple users are hitting the same page with different needs of the dynamic buttons perhaps simultaneous?

Is there any way to do this totally in memory without creating a disk file of any type?

- Mike

You just set the src to yourimagefile.aspx and use yourimage.Save(Response.OutputStream, ImageFormat.Jpeg)
You should also have a Response.ContentType = "image/jpeg" or netscape probably won't interpret it properly.
To draw text try something like the following

Function mgDrawText(objBitmap as bitmap, strText as string, intFontSize as int32, sysColor as color, intHorzPos as int32, intVertPos as int32) as bitmap
dim objGraphic as Graphics = Graphics.FromImage(objBitmap)
objGraphic.DrawString(strText,New Font("Tahoma",intFontSize),New SolidBrush(sysColor),intHorzPos, intVertPos)
return objBitMap
End Function
I need to dynamically create a bitmap (dynamic text) and then output it as the image on a image button.

I can create the bitmap fine and if I output it directly to the output stream all works just fine.

The problem comes in outputting during the render function of the control.

Do I need some html tag to state that the following output stream is really a bitmap stream instead of text?

- Mike
 
V

vMike

You should be able to pass params from a querystring which alter the form of the button instead of just calling the aspx file and have your aspx handle the output based on the querystring. here is some code I use.

sub Page_Load(sender as object, e as eventArgs)

dim strPic as string = request.querystring("_p")
dim strDesigner as string = request.querystring("_d") & ""
dim strSizes as string = request.querystring("_s") & ""
dim intWidth as int32 = ctype(request.querystring("_w"),int32)
dim intHeight as int32 = ctype(request.querystring("_h"),int32)
dim intQuality as int32 = ctype(request.querystring("_q"),int32)
dim myNewImage as system.drawing.image
dim intCacheTime as int32 = ctype(request.querystring("_c"),int32)
if intCacheTime > 3600 then intCacheTime = 3600

dim strFileLoc as string = server.mappath(configurationSettings.AppSettings("picroot"))
Response.ContentType = "image/jpeg"
if intCacheTime > 0 then
Response.Cache.SetExpires(DateTime.Now.AddSeconds(intCacheTime))
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.Cache.SetValidUntilExpires(True)
Response.Cache.VaryByParams("_p") = True
Response.Cache.VaryByParams("_h") = True
Response.Cache.VaryByParams("_c") = True
Response.Cache.VaryByParams("_s") = True
end if


try
select case intQuality
case is = 1
Dim bFlag1 as boolean = false
Dim bFlag2 as boolean = false
if intWidth = 0 then intWidth = 200
if intHeight = 0 then intHeight = 200
if strDesigner.length > 0 then bFlag1 = true
if strSizes.length > 0 then bFlag2 = true
mynewimage = mgAdjustBitmap(strFileLoc & strPic, intWidth, intHeight,bFlag1,strDesigner,bFlag2,strSizes)
case is = 2
if intWidth = 0 then intWidth = 90
if intHeight = 0 then intHeight = 90
mynewimage = mggetthumbnail(strFileLoc & strPic,intWidth,intHeight)
'disabled for security reasons but helpful if you want to save the file to disk

' case is = 3
' if intWidth = 0 then intWidth = 90
' if intHeight = 0 then intHeight = 90
' mynewimage = mggetthumbnail(strFileLoc & strPic,intWidth,intHeight)
' dim strSaveLoc as string = server.mappath(configurationSettings.AppSettings("thumbroot"))
' mynewimage.save(strSaveLoc & strpic,ImageFormat.Jpeg)
case else
if intWidth = 0 then intWidth = 90
if intHeight = 0 then intHeight = 90
mynewimage = mggetthumbnail(strFileLoc & strPic,intWidth,intHeight)

end select
mynewimage.Save(Response.OutputStream, ImageFormat.Jpeg)
mynewimage.dispose
catch
'default if no picture found
mynewimage= mggetthumbnail(strFileLoc & "npicture.jpg",240,180)

mynewimage.Save(Response.OutputStream, ImageFormat.Jpeg)
mynewimage.dispose
end try
End Sub

Function mgGetThumbNail(strFile as string, intWidth as integer, intHeight as integer) as system.drawing.image
Dim myBitmap As bitmap = New Bitmap(strFile)
return mybitmap.getthumbnailimage(intWidth,intHeight,nothing,nothing)
End Function

Function mgAdjustBitmap(strFile as string, intHorz as integer, intVert as integer,bDrawTextFlag1 as boolean, strBottomText as string, bDrawTextFlag2 as boolean, strTopText as string) as bitmap

Dim mysize as size = new size(inthorz,intVert)
Dim myBitmap As bitmap = New Bitmap(strFile)
myBitmap = new bitmap(mybitmap,mysize)
if bDrawTextFlag1 = true and bDrawTextFlag2 = true then
myBitmap = mgDrawText2(myBitmap, strBottomText, strTopText, 8, color.blue,intHorz,intVert)
else
if bDrawTextFlag1 then
myBitmap = mgDrawText(myBitmap, strBottomText, 8, color.blue,ctype(intHorz*.05,int32),ctype(intVert * .93,int32))
end if
if bDrawTextFlag2 then
myBitmap = mgDrawText(myBitmap, strTopText, 8, color.blue,ctype(intHorz*.05,int32),ctype(intVert * .01,int32))
end if
end if
return myBitmap
End Function

Function mgDrawText(objBitmap as bitmap, strText as string, intFontSize as int32, sysColor as color, intHorzPos as int32, intVertPos as int32) as bitmap
dim objGraphic as Graphics = Graphics.FromImage(objBitmap)
objGraphic.DrawString(strText,New Font("Tahoma",intFontSize),New SolidBrush(sysColor),intHorzPos, intVertPos)
return objBitMap
End Function

Function mgDrawText2(objBitmap as bitmap, strBottomText as string,strTopText as string, intFontSize as int32, sysColor as color, intHorz as int32, intVert as int32) as bitmap
dim objGraphic as Graphics = Graphics.FromImage(objBitmap)
objGraphic.DrawString(strBottomText,New Font("Tahoma",intFontSize),New SolidBrush(sysColor),ctype(intHorz*.05,int32), ctype(intVert * ..93,int32))
objGraphic.DrawString(strTopText,New Font("Tahoma",intFontSize),New SolidBrush(sysColor),ctype(intHorz*.05,int32), ctype(intVert * ..01,int32))

return objBitMap
End Function

I've looked at this possibility.

These are dynamically created buttons of varying text. I can dynamically create the button aspx file but, wont I run into a problem when multiple users are hitting the same page with different needs of the dynamic buttons perhaps simultaneous?

Is there any way to do this totally in memory without creating a disk file of any type?

- Mike

You just set the src to yourimagefile.aspx and use yourimage.Save(Response.OutputStream, ImageFormat.Jpeg)
You should also have a Response.ContentType = "image/jpeg" or netscape probably won't interpret it properly.
To draw text try something like the following

Function mgDrawText(objBitmap as bitmap, strText as string, intFontSize as int32, sysColor as color, intHorzPos as int32, intVertPos as int32) as bitmap
dim objGraphic as Graphics = Graphics.FromImage(objBitmap)
objGraphic.DrawString(strText,New Font("Tahoma",intFontSize),New SolidBrush(sysColor),intHorzPos, intVertPos)
return objBitMap
End Function
I need to dynamically create a bitmap (dynamic text) and then output it as the image on a image button.

I can create the bitmap fine and if I output it directly to the output stream all works just fine.

The problem comes in outputting during the render function of the control.

Do I need some html tag to state that the following output stream is really a bitmap stream instead of text?

- Mike
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top