在ASP.net中即时创建ImageButton图像 [英] Create ImageButton image on the fly in ASP.net
问题描述
你好,
是ASP.Net的新手,但可以使用winForm,我天真地以为我可以动态创建一个图像按钮,然后在其上放置一个图像(而不是ImageURL).
问题是这样-
我想在我的网格视图中创建一个图像按钮.我将其放置在设计时,并且每一行都需要基于该行中的某些值的自定义图像.创建图像(使用GDi +)没有问题,但是将其分配给按钮似乎是不可能的.
Hello there,
Being new to ASP.Net but ok with winForm I rather naively thought I could create an image button on the fly and put an image on it (rather than an ImageURL).
The problem is this -
I want to create an imagebutton in my gridview. I am placing this in at design time and each row needs a custom image based on some values in the row. No problem with creating the image (using GDi+) but assigning it to the button seems impossible.
Dim B As New ImageButton
B.CommandArgument = "RowNum"
B.CommandName = "AddMerit"
B.ImageUrl = THIS IS WHERE I NEED TO SET THE IMAGE
R.Cells(2).Controls.Add(B)
如果ImageButton具有"image"属性,那么这将是微不足道的.但是,它只有一个ImageURL.我想我可以创建映像,将其保存在服务器上,然后引用它,但是清理起来很困难而且很麻烦.我试图了解如何使用网络方法来提供图像,但是对于30-40行的表来说,这似乎也很昂贵-我希望在将图像发送到服务器之前对其进行设置....
任何帮助将不胜感激.
If ImageButton had an ''image'' property then this would be trivial. However, it only has an ImageURL. I suppose that I could create the image, save it on the server and then reference it, but this would be difficult and messy to clean up. I have tried to understand how I might use a webmethod to serve the image, but this also seems expensive for a table of 30-40 rows - I would prefer to set the images before they are sent to the server....
Any help would be appreciated.
推荐答案
我找到了一个解决方案,但我不相信它是最佳的!解决方案是使用页面加载中的以下代码构建完整的页面:
I have found a solution but I cannot believe it is optimal! The solution is to build a complete page with the following code in the page load:
Dim FinalBitmap As Bitmap
Dim NumMerits = Request.Params("Merits").ToString
Dim BarGraphPercent = Request.Params("BarGraph").ToString
Dim msStream As New MemoryStream
'Code here to populate FinalBitmap with the image
FinalBitmap.Save(msStream, ImageFormat.Png)
Response.ContentType = "image/png"
msStream.WriteTo(Response.OutputStream)
If Not IsNothing(FinalBitmap) Then FinalBitmap.Dispose()
然后,在主页中,我将每一行链接到"~/Pages/Register/GetGraph?Merits=3&BarGraph=80"
(或任何学生的图形)
然后,这导致对服务器的30次调用,每个图像一次.当然,还有比这更好的方法!
In the main page I then link each row to a "~/Pages/Register/GetGraph?Merits=3&BarGraph=80"
(or whatever that student''s figures are)
This then results in 30 calls to the server, one for each image. Surely there is a better way than this!
我还没有真正找到一个可以接受的解决方案-次优解决方案可以工作,但是页面加载每张图像都需要花费一定的时间.最后,我为每个标准创建了图像-400张图像-并引用了这些图像.
I have not really found an acceptable solution - the sub optimal solution works, but the page takes ages to load each image. In the end I created images for each criteria - 400 images - and referenced these.
这篇关于在ASP.net中即时创建ImageButton图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!