gpt4 book ai didi

c# - 将图像发布到 Facebook 时指定图像大小

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:48 25 4
gpt4 key购买 nike

我正在使用 C# FaceBook API 将状态更新和图像发布到墙上。我正在使用以下代码:

    public string PostToFaceBookPage(string appKey, string appSecret, string wallId, string postMessage, string imageUrl)
{
string id = "";
if (string.IsNullOrEmpty(imageUrl))
imageUrl = "";
var client = new Facebook.FacebookClient();

dynamic result = client.Post("oauth/access_token",
new
{
client_id = appKey,
client_secret = appSecret,
grant_type = "client_credentials",
redirect_uri = "anywhere",
scope = "publish_stream"
});
client.AccessToken = (result)["access_token"];

if (!IsId(wallId))
{
result = client.Get(wallId);
id = result["id"];
} else
{
id = wallId;
}

if (imageUrl == "")
{
result = client.Post("/" + id + "/feed", new
{
message = postMessage,
scope = "publish_stream",
privacy = "{\"value\": \"EVERYONE\"}"
});
} else
{
var uri = new Uri(imageUrl);
string imageName = Path.GetFileName(uri.LocalPath);
string mimeType = GetMimeType(Path.GetExtension(uri.LocalPath));
var media = new Facebook.FacebookMediaObject
{
FileName = imageName,
ContentType = mimeType
};

media.SetValue(GetImage(imageUrl));
result = client.Post("/" + id + "/feed", new
{
message = postMessage,
source = media,
picture = imageUrl,
scope = "publish_stream",
privacy = "{\"value\": \"EVERYONE\"}"
});
}

return "";
}

一切正常。我唯一的问题是,无论实际图像的大小如何,我的图像都张贴了完全相同的尺寸。有没有办法告诉 FaceBook 图片的大小,这样它就不会只发布图片的小缩略图?

最佳答案

我不确定 Facebook API 是否可以具有这些参数,因为自从我使用该 API 以来已经有几年了。

不过,作为临时解决方案,您可以尝试在将照片上传到 Facebook 之前调整其大小。

您可以使用来自 http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing 的以下代码

private static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);

if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();

return (Image)b;
}

关于c# - 将图像发布到 Facebook 时指定图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12899098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com