Generating Bitmap Of WPF UIElement
Aug 1, 2010
I'm trying to generate a bitmap off a WPF border. The whole thing sits in a asp.net app (server side of course) running .net 4.0. The problem is, that the generated image is, well, empty. Does anyone have an idea why? Here's the code.
public static byte[] Draw(int width, int height)
{
MemoryStream memoryStream
= new MemoryStream();
Thread t = new Thread(delegate()
{
System.Windows.Controls.Border border = new System.Windows.Controls.Border()
{
Background = Brushes.Red,
BorderBrush = Brushes.Green,
CornerRadius = new System.Windows.CornerRadius(5),
Width = width,
Height = height
};
border.ApplyTemplate();
RenderTargetBitmap renderTargetBitmap =
new RenderTargetBitmap(width, height, 90, 90, PixelFormats.Pbgra32);
renderTargetBitmap.Render(border);
BitmapEncoder bitmapEncoder =
new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
bitmapEncoder.Save(memoryStream);
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
bool success = t.Join(5000);
if (success)
return memoryStream.ToArray();
else
throw new Exception("Fail");
}
The result is quite bad, as I said the thing returns the image with the right width and height, but it's empty, so I guess I don't mess the thing up with the threading bit.
View 1 Replies
Similar Messages:
May 5, 2010
I have a colorpicker.png file and track the pixel clicked in codebehind using GetPixel. Works like a charm, but I read that converting to a bitmap on the fly is rather heavy on the server. Therefore I saved my .png file as a .bmp as well, so that a converted file would already exist. However, I don't seem tobe able to just use the bitmap file, but need to create a new bitmap from the bitmap...
My question is: The code below works (I provide it here for those who might need it - simply add the code below to codebehind and this code in your aspx page <asp:ImageButton
ID="cpRubrik"
runat="server"
ImageUrl="~/img/colorpicker.png"
/> ;
also note that the image can be whatever you want - a photo or a screendump of a colorpicker from say Photoshop or VS; and note that it's wise to put everything you need in an updatepanel). Should I write something else in order to get smarter access to the bitmap?
Protected Sub cpRubrik_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles cpRubrik.Click
tbUtseenderub.ForeColor = BytFarg(e.X, e.Y)
End Sub
Protected Function BytFarg(ByVal x As Integer, ByVal y As Integer) As Color
Dim myBitmap As New Bitmap(Server.MapPath("~/img/colorpicker.bmp"))
Dim clr As Color = myBitmap.GetPixel(x, y)
Return clr
End Function
View 1 Replies
Jun 23, 2010
I have an ASP.NET web form. This web form has an event handler that generates some HTML. This HTML is based on the time-of-day, that is why it is created in the event handler. Based on the time of day, an image is created programmatically via the following method:
private Bitmap GetImageForTime(DateTime time)
{
Bitmap bitmap = new Bitmap();
// Dynamically build the bitmap...
return bitmap;
}
want to call this method when the HTML is being generated. However, I do NOT want to write the image on the server. Instead, I would like to figure out a way to write it out along with the HTML. In a sense, I'm trying to accomplish the following:
protected void myLiteral_Load(object sender, EventArgs e)
{
string html = "<table><tr><td>";
html += GetImageForTime(DateTime.Now); // This is the problem because it's binary.
html += "</td><td>";
html += GetWeatherHtmlText();
html += "</td></tr></table>";
myLiteral.Text = html;
}
View 6 Replies
Jan 7, 2010
i am trying to bind image to image control . but image is bitmap data. how can i?
View 1 Replies
Apr 6, 2010
I am generating a System.Drawing.Bitmap on the fly in an ASP.NET Custom Web Server Control, and then I want to serve this bitmap as part of the WebResource, because I do not want to save it on the hosting computer. Is there a way to instruct ASP.NET to serve the generated System.Drawing.Bitmap as part of it's WebResource? (therefore making it an "Embedded Resource")
View 1 Replies
May 19, 2012
I am trying to convert a image (from url) to bitmap here the code i was using but its for using a image from your computer (file) Dim bmp As bitmap = bitmap.FromFile(TextBox1.Text) I entering a url instead of a file location but i get a URI error
View 2 Replies
Feb 10, 2011
i had drawed a graph by clicking a analyze button, however when i click it, it will go to a new screen. below is my coding:
[Code]....
below is the condition i want, when i click the button Analyze, the image (graph) is showed under the button with the header and template:
[Code]....
my current condition is when i click the button analyze, it will totaly display the graph only without the header and template as print screen below: how to change the coding to let the template and heaser showed together with the bar chart?
View 1 Replies
Jan 4, 2010
In ASP.NET C# I'm trying to save a bitmap image as an 16-color non-transparent grayscale image as either a PNG or GIF. I assume I have to create a palette and then somehow attach the palette to the image but not sure how to go about doing this. The source image is a 24-bit color Bitmap.
View 2 Replies
May 10, 2010
I am currently dynamically creating a bitmap and using the graphics object from the string from the bitmap to Draw a string on it like so.
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.DrawString(text, font, brush, new System.Drawing.Point(0, 0));
This returns a rectangle shaped bitmap with the string written straight across from left to right. I would like to also be able to draw the string in the shape of a rainbow. How can I do this?
View 2 Replies
Mar 17, 2010
I had write a code about get a picture from url and save as bitmap. However, there was a problem in HttpWebResponse. It didn't run the code start from HttpWebResponse.Then my whole website die there.
[Code]....
View 8 Replies
Sep 20, 2010
I have to save the webpage as image in my system folder. I am saving them using stream.....but the page is not displayed as Image....It opened only through browsers.
View 2 Replies
Mar 22, 2011
This in in an ASP.NET application.
Locally, this code runs fine, but on our production server, it throws an exception of 'Parameter is not valid' when Bitmap.Save() is called.
Should I not be using System.Drawing.Bitmap because its not recommened based on this:
[URL]
Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.
What else could I use?
Bitmap myBitmap = new Bitmap(img);
myBitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
// get the tiff codec info
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff");
// Create an Encoder object based on the GUID for the Compression parameter category
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Compression;
// create encode parameters
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
// save as a tiff
myBitmap.Save(input, myImageCodecInfo, myEncoderParameters);
View 3 Replies
Oct 19, 2010
I'm making an application which takes image from user upload it to server and then perform any image processing like to convert to gray or invert etc.
My problem is this when i did the process on image to gray it or invert then how to write this new file on server or is there any other way so that i would be able to show this (new) image to user and make it downloadable?
View 2 Replies
Dec 14, 2010
I'm using RaphaelJS. The browser renders a chart for me (which is in VML as all my users are on IE). I want the user to be able to save this image and share it normally eg paste into an email, into powerpoint, into a document etc etc.
Problem is not many things can render VML. I can easily get the VML markup describing the image back to the server. All I want to do is convert it to some kind of more universal format eg PNG, BMP, GIFF, whatever which I can then allow the user to download. I've seen lots of people struggling with this. I would have thought the seeing as VML is Microsoft's proprietary SVG format they might have at least provided facilities within their own languages (C#,VB.net) to convert VML to bitmaps. (Incidentally I can't use PHP - I've seen a lot of people attempting to solve this with a PHP based solution)
View 1 Replies
Apr 2, 2011
I m facing one problem which is related to Bitmap Image Actually My image is saved properly without iis server but when i run application on iis server then its give Exception "A generic error occurred in GDI+."
I m using this code-
Byte[] bytes = (Byte[])ds.Tables[0].Rows[r][1];
MemoryStream memStream = new MemoryStream();
memStream.Write(bytes, 0, bytes.Length);
System.Drawing.Bitmap origBMP = new System.Drawing.Bitmap(memStream);
System.Drawing.Bitmap newBMP = new System.Drawing.Bitmap(origBMP, 100, 150);
System.Drawing.Graphics objGra = System.Drawing.Graphics.FromImage(newBMP);
objGra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
objGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
origBMP.Dispose();
newBMP.Save(Server.MapPath("ImageUpload\" + r + ".Jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg); //exception throw "A generic error occurred in GDI+."
newBMP.Dispose();
objGra.Dispose();
View 3 Replies
May 14, 2010
I am trying to built a WebForm with module where the user can see himself in his webcam. I am using AForge.NET framework to control the webcam.
AForge returns every frame as Bitmap object (System.Drawing.Bitmap) and now i have to display it in my WebForm. In the Windows form I would achieve it by assigning Bitmap to the PictureBox control but how do i do it in WebForm? There is only Image control avilable (System.Web.UI.WebControls.Image) which doesn`t have any .Source propery apart form .Url. How do I display Bitmap object on the page ?
View 2 Replies
May 7, 2010
I have the following .ashx page that takes some query string parameters and returns a bitmap with the specified text written on it. The problem I have is that I am currently just manually setting the initial size of the bitmap at 100 X 100 when what I really want is to have the bitmap be just big enough to include all the text that was written to it. How can I do this?
[Code]....
View 1 Replies
Oct 25, 2010
I am working on a website that draws fractals on the screen. It was my intention that with a right mouse click the picture could be saved to the visitor's computer. But that I get an error message "filetype blocked".The file is called test.jpg and is drawn to the screen as a bitmap. Could anybody tell me how to 'unblock' the right mouse click so that a visitor can save a picture he or she likes?Maybe it would be possible to let people download the image? But how would that work?
View 3 Replies
Oct 5, 2010
How can I to use System.Drawing.Bitmap class into athoer class?
using System.Data;
using System.Collections;
using System.Globalization;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace University
{
public class Class1
{
System.Drawing.Bitmap BMP=new System.Drawing .Bitmap();
}
}
when I use System.Drawing.Bitmap gives error this class does not exists
View 2 Replies
Dec 8, 2010
How can I make a dll of my web application usercontrol?
I have usercontrol222.ascx, but I want to make a dll out of this usercontrol.
View 3 Replies
Jul 2, 2010
I am in need of generating a PDF document (or some other type of formatted document such as an rtf) on the fly from a weg page. I am aware of components such as PDFLIb, which I currently use on a few applications, however, quite expensive. Would anyone be able to suggest another method or component to do so? I need to have good formatting capability and additionally, not have the URL's display on the page generated, whne printed.
View 6 Replies
Jul 7, 2010
I am trying to generate a pdf document using the following code which i picked up from one of these forums. It had worked fine when I exported a gridview to pdf.
Now I need to export HTML code to pdf. Can someone suggest how to proceed, possibly using the gridview exporting code below? Any other approach is also welcome.
Code:
Response.ContentType = "application/pdf";
filename = "DocumentName.pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.B2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
string sFinanceReportpath = ConfigurationManager.AppSettings["UploadFinanceReportPath"].ToString();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(sFinanceReportpath + filename, FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
//Response.Write(pdfDoc);
//Response.End();
View 4 Replies
Jan 6, 2010
I'm trying to redirect to another page by calling an action in controller with a specific parameter. I'm trying to use this line:
window.open('<%= Url.Action("Report", "Survey", new { id = ' + selectedRow + ' } ) %>');
But I couldn't make it work; it gives the following error:
CS1012: Too many characters in character literal.
Can't I generate the action URL this was on the client side? Or do I have to make an Ajax call by supplying the parameter and get back the needed URL? This doesn't seem right, but I want to if it's the only way.
View 5 Replies
Feb 2, 2011
I'm currently running a site of a dedicated server but want to scale up using Microsofts Azure cloud platform in the future but im unsure if a certin part of my code will work on azure.The site consists of a gallery of items with an image for each item.The images are stored in a sqlserver database.
I have created a http handler that caches the images to disk and redirects the request to the image (as shown at the end of the post).The images are saved into a virtual directory called "imagecache" inside my ASP.NET application. (i.e. ~/imagecache/ ).
As the web app will run in many virtual machine instances on the Azure platform the images will need to be shared between the instances right?So my question really is.. What is the best way of achieving what I already have on that is compatible wit azure?Image gen code..
public class getimage : IHttpHandler {
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void ProcessRequest (HttpContext context) { [code]....
View 2 Replies
Oct 20, 2010
I have 3 tables(Status,Clist,longlead) having "WOnumber" common column in 3 tables and 3 web forms(status.aspx,Clist.aspx,longlead.aspx), on main page(status.aspx) i have buttons to redirect the clist.aspx and longlead.aspx to give the inputs and save it to database.I have button called scheduleReport on status.aspx when i click this button I want the values from Clist table and longlead table of corresponding WONumber values should be listed either in word or pdf.on the top of the report it should have WONUmber.
View 2 Replies