Pesquisa

segunda-feira, 3 de janeiro de 2011

Imagem com cantos arredondados

Para fazer imagens com cantos arredondados sem que aconteça problemas em diferentes navegadores, basta renderizar as imagens no servidor.

Segue abaixo um exemplo de como fazer:

Pagina RoundCorner.aspx:

Página responsável por ajustar os cantos das imagens.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>

<script runat="server">

void Page_Init(Object sender,EventArgs e) {

string imagem = Request.QueryString["Imagem"].ToString();
string path = Server.MapPath(String.Format("~/{0}", imagem));
int roundedDia = 20;

using(System.Drawing.Image imgin = System.Drawing.Image.FromFile(path)){

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imgin.Width, imgin.Height);
Graphics g = Graphics.FromImage(bitmap);

//Define a cor do canto arredondado
Color cor = Color.FromArgb(29, 29, 29);
g.Clear(cor);

Brush brush = new System.Drawing.TextureBrush(imgin);

FillRoundedRectangle(g, new Rectangle(0, 0, imgin.Width, imgin.Height), roundedDia, brush);

// done with drawing dispose graphics object.

g.Dispose();

// Stream Image to client.

Response.Clear();

Response.ContentType = "image/pjpeg";

bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End();

// dispose bitmap object.

bitmap.Dispose();
}
}

public static void FillRoundedRectangle(Graphics g,Rectangle r,int d,Brush b){

// anti alias distorts fill so remove it.
System.Drawing.Drawing2D.SmoothingMode mode = g.SmoothingMode ;

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
g.FillPie(b,r.X,r.Y,d,d,180,90);
g.FillPie(b,r.X+r.Width-d,r.Y,d,d,270,90);
g.FillPie(b,r.X ,r.Y+r.Height -d,d,d,90,90);
g.FillPie(b,r.X +r.Width-d,r.Y+r.Height -d,d,d,0,90);
g.FillRectangle(b,r.X + d/2,r.Y,r.Width-d,d/2);
g.FillRectangle(b,r.X ,r.Y +d/2,r.Width,r.Height-d);
g.FillRectangle(b,r.X + d/2,r.Y + r.Height-d/2,r.Width-d,d/2);
g.SmoothingMode = mode;
}

</script>

Página que utiliza a RoundCorner.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image runat="server" ID="img" ImageUrl="~/RoundCorner.aspx?Imagem=imagem.jpg" />
</div>
</form>
</body>
</html>

Créditos: Tiago Bandeira


Select em Stored Procedure

Para não ter que reescrever a lógica existente em uma stored procedure para fazer um simples select, basta inserir o conteudo de uma stored procedure para dentro de uma tabela temporária.

É praticamente como utilizar a stored procedure no lugar da tabela.

Segue abaixo o exemplo:

-- DROP PROCEDURE testProc
-- DROP TABLE #testTable

CREATE PROCEDURE testProc AS
BEGIN
SELECT 'AB' as col1, 'CD' as col2
UNION
SELECT 'EF' as col1, 'GH' as col2
END

GO
CREATE TABLE #testTable (col1 varchar(5), col2 varchar(5))

GO
-- this call will succeed
INSERT INTO #testTable EXEC testProc

GO
SELECT * FROM #testTable where col2 like '%C%'



Gerar imagem com texto

Gerar imagem com texto personalizado.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gerar-imagem.aspx.cs" Inherits="gerar_imagem" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="Text" />
<br>
<br>
<asp:DropDownList runat="server" ID="BackgroundColor">
<asp:ListItem Value="red">Red</asp:ListItem>
<asp:ListItem Value="green">Green</asp:ListItem>
<asp:ListItem Value="navy">Navy</asp:ListItem>
<asp:ListItem Value="orange">Orange</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="Font">
<asp:ListItem Value="Arial">Arial</asp:ListItem>
<asp:ListItem Value="Verdana">Verdana</asp:ListItem>
<asp:ListItem Value="Courier">Courier</asp:ListItem>
<asp:ListItem Value="Times New Roman">Times New Roman</asp:ListItem>
</asp:DropDownList>
<br>
<br>
<asp:Button runat="Server" ID="SubmitButton" Text="Gerar Imagem" />
</div>
</form>
</body>
</html>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;

public partial class gerar_imagem : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
Bitmap oBitmap = new Bitmap(468, 60);
Graphics oGraphic = Graphics.FromImage(oBitmap);
System.Drawing.Color oColor = new Color();

String sColor = Request["BackgroundColor"];
String sText = Request["Text"];
String sFont = Request["Font"];

switch (sColor)
{
case "red":
oColor = Color.Red;
break;
case "green":
oColor = Color.Green;
break;
case "navy":
oColor = Color.Navy;
break;
case "orange":
oColor = Color.Orange;
break;
default:
oColor = Color.Gray;
break;
}
SolidBrush oBrush = new SolidBrush(oColor);
SolidBrush oBrushWrite = new SolidBrush(Color.White);

oGraphic.FillRectangle(oBrush, 0, 0, 468, 60);
oGraphic.TextRenderingHint = TextRenderingHint.AntiAlias;

Font oFont = new Font(sFont, 13);
PointF oPoint = new PointF(5, 5);

oGraphic.DrawString(sText, oFont, oBrushWrite, oPoint);

oBitmap.Save(Server.MapPath("gen_img.jpg"), ImageFormat.Jpeg);

Response.Write("Imagem gerada <a target=\"_blank\" href=\"gen_img.jpg\">aqui</a>");
}
}
}



Desabilitar botão após o click

Solução muito simples para desabilitar o botão após o click.

Essa solução pode ser utilizado em requisições ajax, com update panel.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" AsyncPostBackTimeout="0" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel runat="server" ID="upPrincipal">
<ContentTemplate>
<asp:Button runat="server" ID="btnSalvar" Text="Salvar"
onclick="btnSalvar_Click" />
<asp:Literal runat="server" ID="ltlTexto"></asp:Literal>
</ContentTemplate>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>

E no code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnSalvar.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(btnSalvar, String.Empty).ToString());
}
protected void btnSalvar_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
ltlTexto.Text += DateTime.Now.ToString() + " - ";
}
}